Skip to content

Commit

Permalink
work in progress code for detecting unused bytes in songs
Browse files Browse the repository at this point in the history
  • Loading branch information
Awuwunya committed Apr 24, 2020
1 parent 0a1e2e7 commit 4feb7d8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 36 deletions.
90 changes: 59 additions & 31 deletions SMPS2ASMv2/ConvertSMPS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ConvertSMPS {
// and these are conversion
public S2AScript scr;
public List<OffsetString> Lables, Lines;
public List<uint> UnunsedChk;
public byte[] data; // data of the file
public uint pos = 0; // current position in the script
public bool followlables = false;// set to true, if we also want to follow new lables
Expand Down Expand Up @@ -144,6 +145,7 @@ public ConvertSMPS(string filebin, string fileasm, string lable) {
public void Convert(S2AScript scr) {
if (debug) Debug("Prepare conversion");
this.scr = scr;
UnunsedChk = new List<uint>();
Lables = new List<OffsetString>();
Lines = new List<OffsetString>();
data = File.ReadAllBytes(filein);
Expand All @@ -155,6 +157,29 @@ public void Convert(S2AScript scr) {
string[] a = null;
if (debug) Debug("--> Start conversion with subscript ''");
ConvertRun(scr.subscripts[""].Items, ref a, baselable, out bool asses, out string c);

// convert unused data
if (scr.subscripts.ContainsKey("unused")) {
List<GenericScriptItem> uscr = scr.subscripts["unused"].Items;

for(int i = 0;i < UnunsedChk.Count;i ++) {
uint p = UnunsedChk[i];

// check if this is not used
if (skipped[p]) continue;

foreach(OffsetString o in Lines) {
if (o.offset <= p && o.offset + o.length >= p)
goto next;
}

// unused, deal with it
pos = p;
ConvertRun(uscr, ref a, baselable, out asses, out c);

next:;
}
}
if (debug) Debug(new string('-', 80));
}

Expand Down Expand Up @@ -220,22 +245,22 @@ public GenericScriptItem[] Combine(GenericScriptItem[][] luts) {
return rout;
}

private void Convert(string label, string lastlable, GenericScriptItem[] LUT, List<GenericScriptItem>[] run, bool str, out string text) {
private void Convert(string label, string lastlable, GenericScriptItem[] LUT, List<GenericScriptItem>[] run, bool str, bool single, out string text) {
text = null;
// init some vars
InitConvertVars();

if (label != "") {
foreach (OffsetString o in GetLablesRule(label, lastlable)) {
Convert(o, LUT, run, str, out text);
Convert(o, LUT, run, str, single, out text);
}
} else Convert(new OffsetString(offset, 0, null), LUT, run, str, out text);
} else Convert(new OffsetString(pos + offset, 0, null), LUT, run, str, single, out text);
}

private GenericScriptItem[] StoredLUT = null;
private List<GenericScriptItem>[] StoredRun = null;

private void Convert(OffsetString o, GenericScriptItem[] LUT, List<GenericScriptItem>[] run, bool str, out string text) {
private void Convert(OffsetString o, GenericScriptItem[] LUT, List<GenericScriptItem>[] run, bool str, bool single, out string text) {
// empty list to be ref'd later (idk =/ )
string[] args = null;
text = null;
Expand Down Expand Up @@ -267,7 +292,7 @@ private void Convert(OffsetString o, GenericScriptItem[] LUT, List<GenericScript
StoredRun = run;
}

while (true) {
do {
if (ProcessItem(LUT, str, o.line, out bool stop, out text)) {
if (stop) break;

Expand All @@ -283,7 +308,7 @@ private void Convert(OffsetString o, GenericScriptItem[] LUT, List<GenericScript

if (stop) break;
}
}
} while (!single);
}

// check if the string is in a valid location
Expand Down Expand Up @@ -324,7 +349,10 @@ private void ConvertRun(List<GenericScriptItem> s, ref string[] args, string las

foreach(GenericScriptItem i in s) {
ProcessItem(i, ref args, lastlable, out stop, out comment);
if (stop) break;
if (stop) {
UnunsedChk.Add(pos);
break;
}
}
}

Expand Down Expand Up @@ -389,6 +417,27 @@ private void ProcessItem(GenericScriptItem i, ref string[] args, string lastlabl
cvterror(i, "Type of item is NULL! This is most likely a programming error in SMPS2ASM!");
return;

case ScriptItemType.Executable: {
ScriptExecute sex = (i as ScriptExecute);
List<GenericScriptItem[]> opt = new List<GenericScriptItem[]>();
List<List<GenericScriptItem>> dir = new List<List<GenericScriptItem>>();

for (int si = 0;si < sex.names.Length;si++) {
ScriptArray sa = scr.GetSubscript(sex.names[si]);
if (sa == null) cvterror(i, "Execute command requested subscript '" + sex.names[si] + "', which does not exist.");

if (sex.types[si]) {
if (sa.Optimized == null) sa.Optimize();
opt.Add(sa.Optimized);

} else dir.Add(sa.Items);
}

if (debug) Debug(pos + offset, i.line, i.identifier, '/' + sex.label);
Convert(sex.label, lastlable, Combine(opt.ToArray()), dir.ToArray(), false, sex.singlemode, out string fuck);
}
break;

case ScriptItemType.Equate:
(i as ScriptEquate).Evaluate();
if (debug) Debug(pos + offset, i.line, i.identifier, '='+ (i as ScriptEquate).GetName() + ' '+ (i as ScriptEquate).val +' '+ (i as ScriptEquate).GetValue());
Expand Down Expand Up @@ -488,27 +537,6 @@ private void ProcessItem(GenericScriptItem i, ref string[] args, string lastlabl
stop = true;
break;

case ScriptItemType.Executable: {
ScriptExecute sex = (i as ScriptExecute);
List<GenericScriptItem[]> opt = new List<GenericScriptItem[]>();
List<List<GenericScriptItem>> dir = new List<List<GenericScriptItem>>();

for (int si = 0;si < sex.names.Length;si++) {
ScriptArray sa = scr.GetSubscript(sex.names[si]);
if (sa == null) cvterror(i, "Execute command requested subscript '" + sex.names[si] + "', which does not exist.");

if (sex.types[si]) {
if (sa.Optimized == null) sa.Optimize();
opt.Add(sa.Optimized);

} else dir.Add(sa.Items);
}

if (debug) Debug(pos + offset, i.line, i.identifier, '/' + sex.label);
Convert(sex.label, lastlable, Combine(opt.ToArray()), dir.ToArray(), false, out string fuck);
}
break;

case ScriptItemType.Import:
if (debug) Debug(pos + offset, i.line, i.identifier, '?' + (i as ScriptImport).name + ';');
ConvertRun(scr.GetSubscript((i as ScriptImport).name).Items, ref args, lastlable, out stop, out comment);
Expand All @@ -525,15 +553,15 @@ private void ProcessItem(GenericScriptItem i, ref string[] args, string lastlabl
byte[] dat = data;
uint pos3 = pos;
bool fol = followlables;
pos -= (uint)((args.Length - 1) - am.num);
pos = 0;
data = new byte[] { Parse.BasicByte(args[am.num]) };

// debug and optimize array
if (debug) Debug(pos + offset, i.line, i.identifier, ":?" + am.num + ' ' + toHexString(data[0], 2) + ' ' + toHexString(pos, 4));
if (am.Inner.Optimized == null) am.Inner.Optimize();

// process request
Convert("", lastlable, am.Inner.Optimized, null, true, out args[am.num]);
Convert("", lastlable, am.Inner.Optimized, null, true, false, out args[am.num]);
followlables = fol;
pos = pos3;
data = dat;
Expand Down Expand Up @@ -600,7 +628,7 @@ private void ProcessItem(GenericScriptItem i, ref string[] args, string lastlabl

if (followlables && lab != null && (uint)lab.offset - offset < skipped.Length && !skipped[(uint)lab.offset - offset]) {
uint pos2 = pos;
Convert(lab, StoredLUT, StoredRun, false, out string fuck);
Convert(lab, StoredLUT, StoredRun, false, false, out string fuck);
pos = pos2;
}

Expand Down
13 changes: 11 additions & 2 deletions SMPS2ASMv2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,22 @@ static void Main(string[] args) {
string[] a = args;

//check if we have a debug option
if(args.Length > 0 && args[0] == "-d") {
opcheck:
if (args.Length > 0 && args[0] == "-d") {
args = args.Skip(1).ToArray();
debug = true;
goto opcheck;
}

//check if we have a pause option
if (args.Length > 0 && args[0] == "-p") {
args = args.Skip(1).ToArray();
pause = true;
goto opcheck;
}

//check if a script file was dragged in
if(args.Length > 0) {
if (args.Length > 0) {
if(File.Exists(args[0]) && args[0].EndsWith(".smpss")) {
string script = args[0];
args = args.Skip(1).ToArray();
Expand Down
7 changes: 5 additions & 2 deletions SMPS2ASMv2/S2AScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,13 @@ private void ParseScript(string data, string[] args, int lnoffs, ref Stack<Scrip

case '/':
// get the label only if there is a space char
bool singlemode = line.Length > 1 && line.ElementAt(1) == '/';
line = line.Substring(singlemode ? 2 : 1);

string lbl = "";
int ind;
if ((ind = line.IndexOf(' ')) != -1) {
lbl = line.Substring(1, ind - 1);
lbl = line.Substring(0, ind);
line = line.Substring(ind + 1);
}

Expand All @@ -168,7 +171,7 @@ private void ParseScript(string data, string[] args, int lnoffs, ref Stack<Scrip
line = line.Substring(last);
}

stack.Peek().Add(new ScriptExecute(lnum, stack.Peek(), lbl, types.ToArray(), names.ToArray()));
stack.Peek().Add(new ScriptExecute(lnum, stack.Peek(), lbl, types.ToArray(), names.ToArray(), singlemode));

// write debug info
if (debug) {
Expand Down
4 changes: 3 additions & 1 deletion SMPS2ASMv2/ScriptItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ public class ScriptExecute : GenericScriptItem {
public string label;
public bool[] types; // true if optimizing, false if not
public string[] names;
public bool singlemode; // if true, scripts and LUT's only execute once

public ScriptExecute(uint lnum, ScriptArray parent, string lbl, bool[] types, string[] names) : base(lnum, parent, "EXE", ScriptItemType.Executable) {
public ScriptExecute(uint lnum, ScriptArray parent, string lbl, bool[] types, string[] names, bool singlemode = false) : base(lnum, parent, "EXE", ScriptItemType.Executable) {
label = lbl;
this.types = types;
this.names = names;
this.singlemode = singlemode;
}
}

Expand Down

0 comments on commit 4feb7d8

Please sign in to comment.