-
Notifications
You must be signed in to change notification settings - Fork 172
/
PersistentInfos.cs
201 lines (168 loc) · 5.34 KB
/
PersistentInfos.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using UnityEngine;
using KBEngine;
using System;
using System.IO;
using System.Text;
using System.Collections;
namespace KBEngine
{
/*
持久化引擎协议,在检测到协议版本发生改变时会清理协议
*/
public class PersistentInfos
{
string _persistentDataPath = "";
bool _isGood = false;
string _digest = "";
public PersistentInfos(string path)
{
_persistentDataPath = path;
installEvents();
_isGood = loadAll();
}
void installEvents()
{
}
public bool isGood()
{
return _isGood;
}
string _getSuffixBase()
{
return KBEngineApp.app.clientVersion + "." + KBEngineApp.app.clientScriptVersion + "." +
KBEngineApp.app.getInitArgs().ip + "." + KBEngineApp.app.getInitArgs().port;
}
string _getSuffix()
{
return _digest + "." + _getSuffixBase();
}
public bool loadAll()
{
byte[] kbengine_digest = loadFile (_persistentDataPath, "kbengine.digest." + _getSuffixBase(), false);
if(kbengine_digest.Length <= 0)
{
clearMessageFiles();
return false;
}
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
_digest = encoding.GetString(kbengine_digest);
byte[] loginapp_onImportClientMessages = loadFile (_persistentDataPath, "loginapp_clientMessages." + _getSuffix(), false);
byte[] baseapp_onImportClientMessages = loadFile (_persistentDataPath, "baseapp_clientMessages." + _getSuffix(), false);
byte[] onImportServerErrorsDescr = loadFile (_persistentDataPath, "serverErrorsDescr." + _getSuffix(), false);
byte[] onImportClientEntityDef = loadFile (_persistentDataPath, "clientEntityDef." + _getSuffix(), false);
if(loginapp_onImportClientMessages.Length > 0 && baseapp_onImportClientMessages.Length > 0)
{
try
{
if(!KBEngineApp.app.importMessagesFromMemoryStream (loginapp_onImportClientMessages,
baseapp_onImportClientMessages, onImportClientEntityDef, onImportServerErrorsDescr))
clearMessageFiles();
return false;
}
catch(Exception e)
{
Dbg.ERROR_MSG("PersistentInfos::loadAll(): is error(" + e.ToString() + ")!");
clearMessageFiles();
return false;
}
}
return true;
}
public void onImportClientMessages(string currserver, byte[] stream)
{
if(currserver == "loginapp")
createFile (_persistentDataPath, "loginapp_clientMessages." + _getSuffix(), stream);
else
createFile (_persistentDataPath, "baseapp_clientMessages." + _getSuffix(), stream);
}
public void onImportServerErrorsDescr(byte[] stream)
{
createFile (_persistentDataPath, "serverErrorsDescr." + _getSuffix(), stream);
}
public void onImportClientEntityDef(byte[] stream)
{
createFile (_persistentDataPath, "clientEntityDef." + _getSuffix(), stream);
}
public void onVersionNotMatch(string verInfo, string serVerInfo)
{
clearMessageFiles();
}
public void onScriptVersionNotMatch(string verInfo, string serVerInfo)
{
clearMessageFiles();
}
public void onServerDigest(string currserver, string serverProtocolMD5, string serverEntitydefMD5)
{
// 我们不需要检查网关的协议, 因为登录loginapp时如果协议有问题已经删除了旧的协议
if(currserver == "baseapp")
{
return;
}
if(_digest != serverProtocolMD5 + serverEntitydefMD5)
{
_digest = serverProtocolMD5 + serverEntitydefMD5;
clearMessageFiles();
}
else
{
return;
}
if(loadFile(_persistentDataPath, "kbengine.digest." + _getSuffixBase(), false).Length == 0)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
createFile(_persistentDataPath, "kbengine.digest." + _getSuffixBase(), encoding.GetBytes(serverProtocolMD5 + serverEntitydefMD5));
}
}
public void clearMessageFiles()
{
deleteFile(_persistentDataPath, "kbengine.digest." + _getSuffixBase());
deleteFile(_persistentDataPath, "loginapp_clientMessages." + _getSuffix());
deleteFile(_persistentDataPath, "baseapp_clientMessages." + _getSuffix());
deleteFile(_persistentDataPath, "serverErrorsDescr." + _getSuffix());
deleteFile(_persistentDataPath, "clientEntityDef." + _getSuffix());
KBEngineApp.app.resetMessages();
}
public void createFile(string path, string name, byte[] datas)
{
deleteFile(path, name);
Dbg.DEBUG_MSG("createFile: " + path + "/" + name);
FileStream fs = new FileStream (path + "/" + name, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write (datas, 0, datas.Length);
fs.Close ();
fs.Dispose ();
}
public byte[] loadFile(string path, string name, bool printerr)
{
FileStream fs;
try{
fs = new FileStream (path + "/" + name, FileMode.Open, FileAccess.Read);
}
catch (Exception e)
{
if(printerr)
{
Dbg.ERROR_MSG("loadFile: " + path + "/" + name);
Dbg.ERROR_MSG(e.ToString());
}
return new byte[0];
}
byte[] datas = new byte[fs.Length];
fs.Read (datas, 0, datas.Length);
fs.Close ();
fs.Dispose ();
Dbg.DEBUG_MSG("loadFile: " + path + "/" + name + ", datasize=" + datas.Length);
return datas;
}
public void deleteFile(string path, string name)
{
//Dbg.DEBUG_MSG("deleteFile: " + path + "/" + name);
try{
File.Delete(path + "/"+ name);
}
catch (Exception e)
{
Debug.LogError(e.ToString());
}
}
}
}