I decided to post a couple core functions from Xkas GUI here for good measure.
RemoveCommentsAndTrim
trims up an ASM patch so it can be more easily processed
CountBytes
Counts the bytes in an ASM patch, starting at a given line and ending at the next ORG or the end of the file
GetFreeSpace
Searches for a given number of unprotected bytes in a ROM, starting in bank 10. Returns "not" if there isn't a string of free bytes long enough, "exp" if the ROM hasn't been expanded, or returns the hex address of the freespace.
mRecreateResourses
A great code I found somewhere on the internet. It re-creates a given resource in the folder of the tool.
...yep, that's about all for now.
One thing I am constantly annoyed at about SMWC is that if I try to index an array with int i, SMWC reads it as an italics tag.
Ersan to the rescue with [!
RemoveCommentsAndTrim
trims up an ASM patch so it can be more easily processed
Code
private string[] removeCommentsAndTrim(string[] input) { for (int i = 0; i < input.Length; i++) { if (input[i].Contains(";")) { input[i] = input[i].Substring(0, input[i ].IndexOf(";")); } if (input[i].Contains("//")) { input[i] = input[i].Substring(0, input[i].IndexOf("//")); } input[i] = input[i].Trim(); } return input; }
CountBytes
Counts the bytes in an ASM patch, starting at a given line and ending at the next ORG or the end of the file
Code
private int countbytes(string asmpath, int startline) { int n = 0; string[] a = File.ReadAllLines(asmpath); a = removeCommentsAndTrim(a); for (int i = startline; i < a.Length && !a[ i].ToLowerInvariant().Contains("org"); i++) { a[i] = a[i].ToLowerInvariant(); if (a[i].Contains(":")) continue; if (a[i].ToLowerInvariant().Contains("incbin")) { n += File.ReadAllBytes(asmpath.Substring(0, asmpath.LastIndexOf("\\") + 1) + a[i].Substring(7)).Length; continue; } if (a[i].ToLowerInvariant().Contains("incsrc")) { n += countbytes(asmpath.Substring (0, asmpath.LastIndexOf("\\") + 1) + a[i].Substring(7), 0); continue; // 1 2 /2 /1 } int fair = 0; for (int j = 0; j < a[i].Length; j++) { if (a[i].Substring(j, 1).IndexOfAny(letters) != -1) { fair++; if (fair >= 3) { fair = 0; n++; } } else fair = 0; } fair = 0; for (int j = 0; j < a[i ].Length; j++) { if (a[i].Substring(j, 1).IndexOfAny(hex) != -1) { fair++; if (fair >= 2) { fair = 0; n++; } } else fair = 0; } } return n; }
GetFreeSpace
Searches for a given number of unprotected bytes in a ROM, starting in bank 10. Returns "not" if there isn't a string of free bytes long enough, "exp" if the ROM hasn't been expanded, or returns the hex address of the freespace.
Code
private string getfreespace(string rompath, int bytesneeded) { byte[] rombyte = File.ReadAllBytes(rompath); if (rombyte.Length < 513 * 1024) return "exp"; int header = 0; if (rombyte[11] == 0 && rombyte[12] == 0 && rombyte[13] == 0) header = 0x200; int freebytes = 0; int freespace = 0x80000 + header; for (int i = freespace; i < rombyte.Length; i++) { if (rombyte[i] == 0x53) { if (rombyte[i + 1] == 0x54) { if (rombyte[i + 2] == 0x41) { if ((rombyte[i + 3] == 0x52) || (((i - header) & 0x77FFF) | 0x8000) == 0x8000) { if (freebytes >= bytesneeded) { rombyte[freespace] = 0x52; rombyte[freespace + 1] = 0x41; rombyte[freespace + 2] = 0x54; rombyte[freespace + 3] = 0x53; rombyte[freespace + 4] = (byte)(bytesneeded & 0xFF); rombyte[freespace + 5] = (byte)(bytesneeded >> 8 & 0xFF); rombyte[freespace + 6] = (byte)(bytesneeded ^ 0xFFFF & 0xFF); rombyte[freespace + 7] = (byte)(bytesneeded ^ 0xFFFF >> 8 & 0xFF); File.WriteAllBytes(rompath, rombyte); string freestring = String.Format("{0:X}", PCSNES(freespace, header)); while (freestring.Length < 6) { freestring = "0" + freestring; } return freestring; } freebytes = 0; i += 5 + rombyte[i + 4] + (rombyte[i + 5] * 0x100); freespace = i; } } } } freebytes++; } if (freespace >= bytesneeded) { rombyte[freespace] = 0x52; rombyte[freespace + 1] = 0x41; rombyte[freespace + 2] = 0x54; rombyte[freespace + 3] = 0x53; rombyte[freespace + 4] = (byte)(bytesneeded & 0xFF); rombyte[freespace + 5] = (byte)(bytesneeded >> 8 & 0xFF); rombyte[freespace + 6] = (byte)(bytesneeded ^ 0xFFFF & 0xFF); rombyte[freespace + 7] = (byte)(bytesneeded ^ 0xFFFF >> 8 & 0xFF); File.WriteAllBytes(rompath, rombyte); string freestring = String.Format("{0:X}", PCSNES(freespace, header)); while (freestring.Length < 6) { freestring = "0" + freestring; } return freestring; } return "not"; }
mRecreateResourses
A great code I found somewhere on the internet. It re-creates a given resource in the folder of the tool.
Code
private void mRecreateResource(string filename) { // Get Current Assembly refrence Assembly currentAssembly = Assembly.GetExecutingAssembly(); // Get all imbedded resources string[] arrResources = currentAssembly.GetManifestResourceNames(); foreach (string resourceName in arrResources) { if (resourceName == filename) { //Name of the file saved on disk string saveAsName = AppDomain.CurrentDomain.BaseDirectory + filename; FileInfo fileInfoOutputFile = new FileInfo(saveAsName); //CHECK IF FILE EXISTS AND DO SOMETHING DEPENDING ON YOUR NEEDS if (fileInfoOutputFile.Exists) { File.Delete(fileInfoOutputFile.ToString()); } //OPEN NEWLY CREATING FILE FOR WRITTING FileStream streamToOutputFile = fileInfoOutputFile.OpenWrite(); //GET THE STREAM TO THE RESOURCES Stream streamToResourceFile = currentAssembly.GetManifestResourceStream(resourceName); //--------------------------------- //SAVE TO DISK OPERATION //--------------------------------- const int size = 4096; byte[] bytes = new byte[4096]; int numBytes; while ((numBytes = streamToResourceFile.Read(bytes, 0, size)) > 0) { streamToOutputFile.Write(bytes, 0, numBytes); } streamToOutputFile.Close(); streamToResourceFile.Close(); }//end_if }//end_foreach }
...yep, that's about all for now.
Ersan to the rescue with [!