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