Rev 1674 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1307 | chris | 1 | using System; |
| 2 | using System.Collections.Generic; |
||
| 3 | using System.Linq; |
||
| 4 | using System.Text; |
||
| 5 | using System.Threading.Tasks; |
||
| 1493 | chris | 6 | using System.Threading; |
| 1423 | chris | 7 | using System.IO; |
| 1493 | chris | 8 | using System.Globalization; |
| 1307 | chris | 9 | |
| 1308 | chris | 10 | using BurutaruEditor.file.elements; |
| 1432 | chris | 11 | using BurutaruEditor.file.enemies; |
| 1308 | chris | 12 | |
| 1423 | chris | 13 | using BauzoidNET.parser; |
| 1607 | chris | 14 | using BauzoidNET.math; |
| 1423 | chris | 15 | |
| 1307 | chris | 16 | namespace BurutaruEditor.file |
| 17 | { |
||
| 18 | public class LevelUtil |
||
| 19 | { |
||
| 20 | private LevelUtil() { } |
||
| 21 | |||
| 1423 | chris | 22 | public static bool LoadLevel(String filename, Document doc) |
| 1307 | chris | 23 | { |
| 1423 | chris | 24 | string text = Preprocessor.stripComments(File.ReadAllText(filename)); |
| 25 | |||
| 26 | Tokenizer t = new Tokenizer(text); |
||
| 27 | t.setStringDelimiter(new char[] { '\'', '"' }); |
||
| 28 | |||
| 29 | while (!t.checkNoMoreTokens()) |
||
| 30 | { |
||
| 1494 | chris | 31 | parseData(doc, t); |
| 32 | /*String identifier = t.readIdentifier(); |
||
| 1423 | chris | 33 | |
| 34 | // name |
||
| 35 | if (doc.ReadParameter(t, identifier)) |
||
| 36 | { |
||
| 37 | continue; |
||
| 38 | } |
||
| 39 | // enemy spawnification |
||
| 40 | else if (identifier.Equals("spawn", StringComparison.OrdinalIgnoreCase)) |
||
| 41 | { |
||
| 42 | parseSpawnEnemy(doc, t); |
||
| 43 | } |
||
| 44 | else if (identifier.Equals("CheckPoint", StringComparison.OrdinalIgnoreCase)) |
||
| 45 | { |
||
| 46 | // check checkpoint objects |
||
| 47 | parsecheckPoint(doc, t); |
||
| 48 | } |
||
| 49 | else |
||
| 50 | { |
||
| 51 | // check condition objects |
||
| 52 | /*if (parseConditionObject(levelData, t, identifier)) |
||
| 53 | continue; |
||
| 54 | |||
| 55 | // check command objects |
||
| 56 | if (parseLevelObject(levelData, t, identifier, BaseCommand.class)) |
||
| 57 | continue; |
||
| 1494 | chris | 58 | */ |
| 59 | /* |
||
| 1435 | chris | 60 | |
| 61 | if (parseLevelElement(doc, t, identifier)) |
||
| 62 | continue; |
||
| 1423 | chris | 63 | |
| 64 | //Gdx.app.log(LOG_TAG, identifier + " not found as applicable Level Object!"); |
||
| 65 | t.skipUntilAfterToken("}"); |
||
| 1494 | chris | 66 | }*/ |
| 1423 | chris | 67 | } |
| 68 | |||
| 1307 | chris | 69 | return true; |
| 70 | } |
||
| 71 | |||
| 1494 | chris | 72 | public static bool parsecheckPoint(Document doc, Tokenizer tokenizer) |
| 1307 | chris | 73 | { |
| 1423 | chris | 74 | string checkPointName = tokenizer.readString(); |
| 75 | |||
| 76 | CheckPoint checkPoint = new CheckPoint(doc, checkPointName); |
||
| 77 | |||
| 78 | tokenizer.readToken("{"); |
||
| 79 | while (!tokenizer.checkToken("}")) |
||
| 80 | { |
||
| 81 | String nextIdentifier = tokenizer.readIdentifier(); |
||
| 82 | checkPoint.ReadParameter(tokenizer, nextIdentifier); |
||
| 83 | } |
||
| 84 | tokenizer.readToken("}"); |
||
| 85 | |||
| 86 | doc.AddCheckPoint(checkPoint); |
||
| 87 | |||
| 88 | return false; |
||
| 89 | } |
||
| 90 | |||
| 1494 | chris | 91 | public static void parseSpawnEnemy(Document doc, Tokenizer tokenizer) |
| 1423 | chris | 92 | { |
| 93 | string enemyType = tokenizer.readIdentifier(); |
||
| 94 | string enemyName = tokenizer.readString(); |
||
| 95 | |||
| 96 | // TODO: parse enemy spawn |
||
| 97 | |||
| 1432 | chris | 98 | EnemySpawner enemy = EnemySpawner.createSpawner(doc, enemyType, enemyName); |
| 99 | |||
| 1423 | chris | 100 | tokenizer.readToken("{"); |
| 1432 | chris | 101 | while (!tokenizer.checkToken("}")) |
| 102 | { |
||
| 103 | String nextIdentifier = tokenizer.readIdentifier(); |
||
| 104 | enemy.ReadParameter(tokenizer, nextIdentifier); |
||
| 105 | } |
||
| 1423 | chris | 106 | |
| 1432 | chris | 107 | tokenizer.readToken("}"); |
| 108 | |||
| 109 | doc.AddEnemySpawner(enemy); |
||
| 1435 | chris | 110 | } |
| 1432 | chris | 111 | |
| 1494 | chris | 112 | public static bool parseLevelElement(Document doc, Tokenizer t, string identifier) |
| 1435 | chris | 113 | { |
| 114 | if (!t.checkString()) |
||
| 115 | return false; |
||
| 1423 | chris | 116 | |
| 1435 | chris | 117 | int position = t.getPosition(); |
| 118 | string elementName = t.readString(); |
||
| 119 | |||
| 120 | LevelElement e = LevelElement.CreateElement(doc, identifier, elementName); |
||
| 121 | if (e == null) |
||
| 122 | { |
||
| 123 | // not a level element |
||
| 124 | t.setPosition(position); |
||
| 125 | return false; |
||
| 126 | } |
||
| 127 | |||
| 128 | t.readToken("{"); |
||
| 129 | while (!t.checkToken("}")) |
||
| 130 | { |
||
| 131 | String nextIdentifier = t.readIdentifier(); |
||
| 132 | e.ReadParameter(t, nextIdentifier); |
||
| 133 | } |
||
| 134 | t.readToken("}"); |
||
| 135 | |||
| 1436 | chris | 136 | e.Init(); |
| 137 | |||
| 1435 | chris | 138 | doc.AddLevelElement(e); |
| 139 | |||
| 140 | return true; |
||
| 141 | } |
||
| 142 | |||
| 1423 | chris | 143 | /* |
| 144 | |||
| 145 | private static boolean parseConditionObject(LevelData levelData, Tokenizer tokenizer, String className) throws ScanException |
||
| 146 | { |
||
| 147 | Class<? extends BaseCondition> conditionClass = null; |
||
| 148 | |||
| 149 | try |
||
| 150 | { |
||
| 151 | conditionClass = Class.forName(BaseCondition.class.getPackage().getName() + "." + className).asSubclass(BaseCondition.class); |
||
| 152 | } |
||
| 153 | catch (ClassNotFoundException ex) |
||
| 154 | { |
||
| 155 | // not a BaseCondition |
||
| 156 | return false; |
||
| 157 | } |
||
| 158 | |||
| 159 | String objectName = tokenizer.readString(); |
||
| 160 | BaseCondition instance = null; |
||
| 161 | |||
| 162 | try |
||
| 163 | { |
||
| 164 | instance = conditionClass.getDeclaredConstructor(Level.class, String.class).newInstance(levelData.getLevel(), objectName); |
||
| 165 | } |
||
| 166 | catch (Exception ex) |
||
| 167 | { |
||
| 168 | throw new ScanException(Consts.LOG_TAG, ex.getLocalizedMessage() + " while instantiating " + className); |
||
| 169 | } |
||
| 170 | |||
| 171 | tokenizer.readToken("{"); |
||
| 172 | while (!tokenizer.checkToken("}")) |
||
| 173 | { |
||
| 174 | String nextIdentifier = tokenizer.readIdentifier(); |
||
| 175 | instance.readParameter(tokenizer, nextIdentifier); |
||
| 176 | } |
||
| 177 | tokenizer.readToken("}"); |
||
| 178 | |||
| 179 | levelData.addCondition(instance); |
||
| 180 | |||
| 181 | return true; |
||
| 182 | } |
||
| 183 | |||
| 184 | */ |
||
| 185 | |||
| 1607 | chris | 186 | public static bool SaveLevelBinary(string filename, Document doc) |
| 187 | { |
||
| 188 | BinarySerializer s = new BinarySerializer(filename, Serializer.OpenMode.Write); |
||
| 189 | |||
| 190 | for (int i = 0; i < doc.CheckPoints.Count; i++) |
||
| 191 | { |
||
| 192 | CheckPoint cp = doc.CheckPoints[i]; |
||
| 193 | |||
| 194 | s.Write("CheckPoint"); |
||
| 195 | s.Write(cp.Name); |
||
| 196 | |||
| 197 | cp.WriteData(s); |
||
| 198 | } |
||
| 199 | |||
| 200 | for (int i = 0; i < doc.Spawners.Count; i++) |
||
| 201 | { |
||
| 202 | EnemySpawner es = doc.Spawners[i]; |
||
| 1674 | chris | 203 | |
| 204 | s.Write("spawn"); |
||
| 205 | s.Write(es.GetPropertiesType()); |
||
| 206 | s.Write(es.Name); |
||
| 207 | |||
| 208 | es.WriteData(s); |
||
| 209 | |||
| 1607 | chris | 210 | /*tw.WriteLine("spawn " + es.GetPropertiesType() + " \"" + es.Name + "\""); |
| 211 | tw.WriteLine("{"); |
||
| 212 | es.WriteData(tw); |
||
| 213 | tw.WriteLine("}"); |
||
| 214 | tw.WriteLine("\r");*/ |
||
| 215 | } |
||
| 216 | |||
| 217 | for (int i = 0; i < doc.LevelElements.Count; i++) |
||
| 218 | { |
||
| 219 | LevelElement le = doc.LevelElements[i]; |
||
| 1674 | chris | 220 | |
| 221 | s.Write(le.GetPropertiesType()); |
||
| 222 | s.Write(le.Name); |
||
| 223 | |||
| 224 | le.WriteData(s); |
||
| 225 | |||
| 1607 | chris | 226 | /*tw.WriteLine(le.GetPropertiesType() + " \"" + le.Name + "\""); |
| 227 | tw.WriteLine("{"); |
||
| 228 | le.WriteData(tw); |
||
| 229 | tw.WriteLine("}"); |
||
| 230 | tw.WriteLine("\r");*/ |
||
| 231 | } |
||
| 232 | |||
| 233 | s.Close(); |
||
| 234 | return true; |
||
| 235 | } |
||
| 236 | |||
| 1423 | chris | 237 | public static bool SaveLevel(String filename, Document doc) |
| 238 | { |
||
| 1675 | chris | 239 | if (Path.GetExtension(filename).Equals("level", StringComparison.OrdinalIgnoreCase)) |
| 240 | { |
||
| 241 | return SaveLevelBinary(filename, doc); |
||
| 242 | } |
||
| 243 | |||
| 1493 | chris | 244 | TextWriter tw = new StreamWriter(filename); |
| 245 | |||
| 246 | Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; |
||
| 247 | |||
| 248 | doc.WriteData(tw); |
||
| 249 | |||
| 250 | for (int i = 0; i < doc.CheckPoints.Count; i++) |
||
| 251 | { |
||
| 252 | CheckPoint cp = doc.CheckPoints[i]; |
||
| 1607 | chris | 253 | |
| 1493 | chris | 254 | tw.WriteLine("CheckPoint \"" + cp.Name + "\""); |
| 255 | tw.WriteLine("{"); |
||
| 256 | cp.WriteData(tw); |
||
| 257 | tw.WriteLine("}"); |
||
| 258 | tw.WriteLine("\r"); |
||
| 259 | } |
||
| 260 | |||
| 261 | for (int i = 0; i < doc.Spawners.Count; i++) |
||
| 262 | { |
||
| 263 | EnemySpawner es = doc.Spawners[i]; |
||
| 264 | tw.WriteLine("spawn " + es.GetPropertiesType() + " \"" + es.Name + "\""); |
||
| 265 | tw.WriteLine("{"); |
||
| 266 | es.WriteData(tw); |
||
| 267 | tw.WriteLine("}"); |
||
| 268 | tw.WriteLine("\r"); |
||
| 269 | } |
||
| 270 | |||
| 271 | for (int i = 0; i < doc.LevelElements.Count; i++) |
||
| 272 | { |
||
| 273 | LevelElement le = doc.LevelElements[i]; |
||
| 274 | tw.WriteLine(le.GetPropertiesType() + " \"" + le.Name + "\""); |
||
| 275 | tw.WriteLine("{"); |
||
| 276 | le.WriteData(tw); |
||
| 277 | tw.WriteLine("}"); |
||
| 278 | tw.WriteLine("\r"); |
||
| 279 | } |
||
| 280 | |||
| 281 | tw.Close(); |
||
| 1307 | chris | 282 | return true; |
| 283 | } |
||
| 284 | |||
| 1494 | chris | 285 | public static void parseData(Document doc, Tokenizer t) |
| 286 | { |
||
| 287 | String identifier = t.readIdentifier(); |
||
| 288 | |||
| 289 | // doc parameters |
||
| 290 | if (doc.ReadParameter(t, identifier)) |
||
| 291 | { |
||
| 292 | return; |
||
| 293 | } |
||
| 294 | // enemy spawnification |
||
| 295 | if (identifier.Equals("spawn", StringComparison.OrdinalIgnoreCase)) |
||
| 296 | { |
||
| 297 | parseSpawnEnemy(doc, t); |
||
| 298 | } |
||
| 299 | else if (identifier.Equals("CheckPoint", StringComparison.OrdinalIgnoreCase)) |
||
| 300 | { |
||
| 301 | // check checkpoint objects |
||
| 302 | parsecheckPoint(doc, t); |
||
| 303 | } |
||
| 304 | else |
||
| 305 | { |
||
| 306 | // check condition objects |
||
| 307 | /*if (parseConditionObject(levelData, t, identifier)) |
||
| 308 | return; |
||
| 309 | |||
| 310 | // check command objects |
||
| 311 | if (parseLevelObject(levelData, t, identifier, BaseCommand.class)) |
||
| 312 | return; |
||
| 313 | */ |
||
| 314 | |||
| 315 | if (parseLevelElement(doc, t, identifier)) |
||
| 316 | return; |
||
| 317 | |||
| 318 | //Gdx.app.log(LOG_TAG, identifier + " not found as applicable Level Object!"); |
||
| 319 | t.skipUntilAfterToken("}"); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | |||
| 324 | public static void CreateObjectFromString(Document doc, string data) |
||
| 325 | { |
||
| 326 | Tokenizer t = new Tokenizer(data); |
||
| 327 | parseData(doc, t); |
||
| 328 | } |
||
| 329 | |||
| 330 | |||
| 331 | public static string GetObjectData(LevelObject obj, string nameAttach = "") |
||
| 332 | { |
||
| 333 | MemoryStream memoryStream = new MemoryStream(); |
||
| 334 | TextWriter tw = new StreamWriter(memoryStream); |
||
| 335 | |||
| 336 | if (obj is CheckPoint) |
||
| 337 | { |
||
| 338 | tw.WriteLine("CheckPoint \"" + obj.Name + nameAttach + "\""); |
||
| 339 | } |
||
| 340 | else if (obj is EnemySpawner) |
||
| 341 | { |
||
| 1496 | chris | 342 | tw.WriteLine("spawn " + obj.GetPropertiesType() + " \"" + obj.Name + nameAttach + "\""); |
| 1494 | chris | 343 | } |
| 344 | else if (obj is LevelElement) |
||
| 345 | { |
||
| 346 | tw.WriteLine(obj.GetPropertiesType() + " \"" + obj.Name + nameAttach + "\""); |
||
| 347 | } |
||
| 348 | |||
| 349 | tw.WriteLine("{"); |
||
| 350 | obj.WriteData(tw); |
||
| 351 | tw.WriteLine("}"); |
||
| 352 | |||
| 353 | tw.Flush(); |
||
| 354 | |||
| 355 | string result = Encoding.ASCII.GetString(memoryStream.ToArray()); |
||
| 356 | |||
| 357 | memoryStream.Close(); |
||
| 358 | |||
| 359 | return result; |
||
| 360 | } |
||
| 361 | |||
| 1607 | chris | 362 | |
| 363 | public static void WriteProperty(TextWriter tw, string id, float value, int numTabs = 0) |
||
| 364 | { |
||
| 365 | for (int i = 0; i < numTabs; i++) |
||
| 366 | tw.Write("\t"); |
||
| 367 | |||
| 368 | tw.WriteLine(id + " " + value.ToString("F") + ";"); |
||
| 369 | } |
||
| 370 | |||
| 371 | public static void WriteProperty(TextWriter tw, string id, int value, int numTabs = 0) |
||
| 372 | { |
||
| 373 | for (int i = 0; i < numTabs; i++) |
||
| 374 | tw.Write("\t"); |
||
| 375 | |||
| 376 | tw.WriteLine(id + " " + value + ";"); |
||
| 377 | } |
||
| 378 | |||
| 379 | public static void WriteProperty(TextWriter tw, string id, string value, int numTabs = 0) |
||
| 380 | { |
||
| 381 | for (int i = 0; i < numTabs; i++) |
||
| 382 | tw.Write("\t"); |
||
| 383 | |||
| 384 | tw.WriteLine(id + " \"" + value + "\";"); |
||
| 385 | } |
||
| 386 | |||
| 387 | public static void WriteProperty(TextWriter tw, string id, bool value, int numTabs = 0) |
||
| 388 | { |
||
| 389 | for (int i = 0; i < numTabs; i++) |
||
| 390 | tw.Write("\t"); |
||
| 391 | |||
| 1624 | chris | 392 | tw.WriteLine(id + " " + (value ? "true" : "false") + ";"); |
| 1607 | chris | 393 | } |
| 394 | |||
| 395 | public static void WriteProperty(TextWriter tw, string id, Vector2 value, int numTabs = 0) |
||
| 396 | { |
||
| 397 | for (int i = 0; i < numTabs; i++) |
||
| 398 | tw.Write("\t"); |
||
| 399 | |||
| 400 | tw.WriteLine(id + " " + value.x.ToString("F") + ", " + value.y.ToString("F") + ";"); |
||
| 401 | } |
||
| 402 | |||
| 1307 | chris | 403 | } |
| 404 | } |