Skip to content

Commit da462f2

Browse files
committed
Fixed a problem with labels
1 parent 240dcf4 commit da462f2

8 files changed

Lines changed: 1805 additions & 1573 deletions

File tree

Assets/Dev/Tiles/TilemapEditor.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,25 +269,20 @@ public void PostLoad() {
269269

270270
IEnumerator Loading() {
271271
string data = Values.text.Trim();
272-
PBar.Show("Loading", 0, 200);
273-
byte[] block;
274-
try {
275-
ByteReader.ReadBlock(data, out List<CodeLabel> labels, out block);
276-
} catch (System.Exception e) {
277-
Values.text = "Parsing error: " + e.Message + "\n" + Values.text;
278-
PBar.Hide();
279-
yield break;
280-
}
281-
282-
yield return PBar.Progress(50);
283-
int pw = block[0];
284-
int ph = block[1];
272+
yield return PBar.Show("Loading", 0, 200);
273+
ByteReaderData res = new ByteReaderData();
274+
StartCoroutine(ByteReader.ReadBlock(data, 0, res));
275+
while (!res.completed)
276+
yield return new WaitForSeconds(.25f);
277+
278+
int pw = res.block[0];
279+
int ph = res.block[1];
285280
MapSizeW.SetValueWithoutNotify(pw);
286281
MapSizeH.SetValueWithoutNotify(ph);
287282
updateMapSize = StartCoroutine(UpdateMapSize(w, h));
288283

289284
// We need to wait for the coroutine to end before proceeding
290-
StartCoroutine(CompleteLoading(block));
285+
StartCoroutine(CompleteLoading(res.block));
291286
}
292287

293288
IEnumerator CompleteLoading(byte[] block) {

Assets/Engine/Arcade.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ private void Start() {
251251
else {
252252
// Load Game.Cartridge
253253
string codefile;
254-
try { codefile = File.ReadAllText(Path.GetDirectoryName(Application.dataPath) + "/Cartridges/Shadow of the Beast.cartridge"); } catch (Exception) {
254+
try { codefile = File.ReadAllText(Path.GetDirectoryName(Application.dataPath) + "/Cartridges/Game.cartridge"); } catch (Exception) {
255255
Write("No cardridge found!", 4, 40, Col.C(5, 1, 0));
256256
Write("Path: " + Path.GetDirectoryName(Application.dataPath) + "/Cartridges/Game.cartridge", 4, 50, 48, 0, 2);
257257
texture.Apply();
@@ -1475,6 +1475,7 @@ internal Value Evaluate(CodeNode n) {
14751475
case BNF.KEYx: return new Value(Input.GetAxis("Horixontal"));
14761476
case BNF.KEYy: return new Value(Input.GetAxis("Vertical"));
14771477

1478+
case BNF.Label:
14781479
case BNF.LAB: {
14791480
if (!labels.ContainsKey(n.sVal)) throw new Exception("Undefined Label: " + n.sVal);
14801481
return new Value(labels[n.sVal]);

Assets/Engine/CodeNode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ internal bool Evaluable() {
390390
case BNF.OPxor:
391391
case BNF.OPlsh:
392392
case BNF.OPrsh:
393+
case BNF.Label:
393394
case BNF.LAB:
394395
case BNF.LABG:
395396
case BNF.UOneg:

Assets/Engine/TODO.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ FUTURE: step by step debugger
1313
FUTURE: map keys
1414

1515

16-
Add progress while saving (tilemaps)
17-
1816
Add a "image" command to draw on the screen a texture (like sprite or tile, no size limit here)
1917
Image(address, posx, posy)
2018

Assets/Engine/Utilities/ByteReader.cs

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,187 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Text.RegularExpressions;
45

6+
public class ByteReaderData {
7+
public List<CodeLabel> labels;
8+
public byte[] block;
9+
public bool completed = false;
10+
}
11+
512
public class ByteReader {
613
public enum ReadMode { Dec, Hex, Bin };
714

815
readonly static Regex rgComments = new Regex("/\\*(?>(?:(?>[^*]+)|\\*(?!/))*)\\*/", RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));
916
readonly static Regex rgComment = new Regex("//(.*?)\r?\n", RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));
1017

18+
internal static IEnumerator ReadBlock(string data, int pbar, ByteReaderData res) {
19+
ReadMode mode = ReadMode.Dec;
20+
data = rgComments.Replace(data, "");
21+
data = rgComment.Replace(data, "");
22+
data = data.Trim().Replace('\r', ' ').Replace('\n', ' ');
23+
while (data.IndexOf(" ") != -1) data = data.Replace(" ", " ");
24+
25+
res.labels = new List<CodeLabel>();
26+
Consolidator consolidator = new Consolidator();
27+
int tot = data.Length;
28+
while (data.Length > 0) {
29+
yield return PBar.Progress(pbar + 100 * (tot - data.Length) / tot);
30+
// Get the part, it is whatever we got until a whitespace
31+
string part = "";
32+
foreach (char c in data) {
33+
if (c == ' ') break;
34+
part += c;
35+
}
36+
part = part.ToLowerInvariant();
37+
// What we have?
38+
if (part[part.Length - 1] == ':') { // Label
39+
res.labels.Add(new CodeLabel { name = part.Substring(0, part.Length - 1).Trim(), start = consolidator.GetPos() });
40+
data = data.Substring(part.Length).Trim();
41+
continue;
42+
}
43+
else if (part.Equals("}")) {
44+
// Consolidate all parts we found
45+
res.block = consolidator.Consolidate();
46+
yield return PBar.Progress(pbar + 100);
47+
res.completed = true;
48+
yield break;
49+
}
50+
else if (part.Equals("usehex")) {
51+
mode = ReadMode.Hex;
52+
data = data.Substring(part.Length).Trim();
53+
continue;
54+
}
55+
else if (part.Equals("usebin")) {
56+
mode = ReadMode.Bin;
57+
data = data.Substring(part.Length).Trim();
58+
continue;
59+
}
60+
else if (part.Equals("usedec")) {
61+
mode = ReadMode.Dec;
62+
data = data.Substring(part.Length).Trim();
63+
continue;
64+
}
65+
// Data
66+
67+
data = data.Substring(part.Length).Trim();
68+
if ((part.Length > 2 && part[0] == '0' && part[1] == 'x') || (part.Length > 1 && part[0] == 'x')) { // Do we start with 0x?
69+
if (part[0] == 'x')
70+
part = part.Substring(1);
71+
else
72+
part = part.Substring(2);
73+
// Get 2 chars as a byte in hex, and continue till the end of the string
74+
while (part.Length > 0) {
75+
string val = part[0] + (part.Length > 1 ? part[1].ToString() : " ");
76+
try {
77+
consolidator.AddByte((byte)Convert.ToInt32(val, 16));
78+
} catch(Exception) {
79+
throw new Exception("Cannot parse \"" + val + "\" as Hex");
80+
}
81+
}
82+
}
83+
else if (part.Length > 2 && part[0] == '0' && part[1] == 'b') { // Do we start with 0b?
84+
part = part.Substring(2);
85+
// Parse all value, then split in bytes
86+
int b;
87+
try {
88+
b = Convert.ToInt32(part, 2);
89+
} catch (Exception) {
90+
throw new Exception("Cannot parse \"" + part + "\" as Binary");
91+
}
92+
if (b < 256) consolidator.AddByte((byte)b);
93+
else if (b < 65536) {
94+
byte l = (byte)(b & 0xff);
95+
byte h = (byte)((b & 0xff00) >> 8);
96+
consolidator.AddByte(h);
97+
consolidator.AddByte(l);
98+
}
99+
else {
100+
byte b0 = (byte)(b & 0xff);
101+
byte b1 = (byte)((b & 0xff00) >> 8);
102+
byte b2 = (byte)((b & 0xff0000) >> 16);
103+
byte b3 = (byte)((b & 0xff000000) >> 24);
104+
consolidator.AddByte(b3);
105+
consolidator.AddByte(b2);
106+
consolidator.AddByte(b1);
107+
consolidator.AddByte(b0);
108+
}
109+
}
110+
111+
if (mode == ReadMode.Dec) { // Parse it as dec, split in bytes
112+
int b;
113+
try {
114+
b = Convert.ToInt32(part, 10);
115+
} catch (Exception) {
116+
PBar.Hide();
117+
throw new Exception("Cannot parse \"" + part + "\" as Decimal");
118+
}
119+
if (b < 256) consolidator.AddByte((byte)b);
120+
else if (b < 65536) {
121+
byte l = (byte)(b & 0xff);
122+
byte h = (byte)((b & 0xff00) >> 8);
123+
consolidator.AddByte(h);
124+
consolidator.AddByte(l);
125+
}
126+
else {
127+
byte b0 = (byte)(b & 0xff);
128+
byte b1 = (byte)((b & 0xff00) >> 8);
129+
byte b2 = (byte)((b & 0xff0000) >> 16);
130+
byte b3 = (byte)((b & 0xff000000) >> 24);
131+
consolidator.AddByte(b3);
132+
consolidator.AddByte(b2);
133+
consolidator.AddByte(b1);
134+
consolidator.AddByte(b0);
135+
}
136+
}
137+
else if (mode == ReadMode.Hex) { // Parse it as hex
138+
while (part.Length > 0) {
139+
string val = part[0] + (part.Length > 1 ? part[1].ToString() : " ");
140+
try {
141+
consolidator.AddByte((byte)Convert.ToInt32(val, 16));
142+
} catch (Exception) {
143+
PBar.Hide();
144+
throw new Exception("Cannot parse \"" + val + "\" as Hex");
145+
}
146+
if (part.Length < 3) break;
147+
part = part.Substring(2);
148+
}
149+
}
150+
else if (mode == ReadMode.Bin) { // Parse it as bin, split in bytes
151+
int b;
152+
try {
153+
b = Convert.ToInt32(part, 2);
154+
} catch (Exception) {
155+
PBar.Hide();
156+
throw new Exception("Cannot parse \"" + part + "\" as Binary");
157+
}
158+
if (b < 256) consolidator.AddByte((byte)b);
159+
else if (b < 65536) {
160+
byte l = (byte)(b & 0xff);
161+
byte h = (byte)((b & 0xff00) >> 8);
162+
consolidator.AddByte(h);
163+
consolidator.AddByte(l);
164+
}
165+
else {
166+
byte b0 = (byte)(b & 0xff);
167+
byte b1 = (byte)((b & 0xff00) >> 8);
168+
byte b2 = (byte)((b & 0xff0000) >> 16);
169+
byte b3 = (byte)((b & 0xff000000) >> 24);
170+
consolidator.AddByte(b3);
171+
consolidator.AddByte(b2);
172+
consolidator.AddByte(b1);
173+
consolidator.AddByte(b0);
174+
}
175+
}
176+
}
177+
yield return PBar.Progress(pbar + 100);
178+
// Consolidate all parts we found
179+
res.block = consolidator.Consolidate();
180+
res.completed = true;
181+
}
182+
183+
184+
11185
internal static void ReadBlock(string data, out List<CodeLabel> labels, out byte[] block) {
12186
ReadMode mode = ReadMode.Dec;
13187
data = rgComments.Replace(data, "");
@@ -65,7 +239,7 @@ internal static void ReadBlock(string data, out List<CodeLabel> labels, out byte
65239
string val = part[0] + (part.Length > 1 ? part[1].ToString() : " ");
66240
try {
67241
consolidator.AddByte((byte)Convert.ToInt32(val, 16));
68-
} catch(Exception) {
242+
} catch (Exception) {
69243
throw new Exception("Cannot parse \"" + val + "\" as Hex");
70244
}
71245
}

0 commit comments

Comments
 (0)