public void InitApi(LuaInterpreter script) { foreach (var unit in units) { unit.DefineApi(script); } script.AddFunction("printh", (Action <object>)Printh); script.AddFunction("stat", (Func <object>)Stat); script.AddFunction("menuitem", (Action <int, string, object>)Menuitem); script.RunScript(LuaPatcher.PatchCode(Api.All)); }
private bool LoadTextCart(string path) { var streamReader = new StreamReader(path); var stateMap = new Dictionary <string, int> { { "__lua__", 0 }, { "__gfx__", 1 }, { "__gff__", 2 }, { "__map__", 3 }, { "__sfx__", 4 }, { "__music__", 5 }, { "__label__", 6 } }; loaded = new Cartridge(); loaded.path = path; var state = -1; var index = 0; var codeBuilder = new StringBuilder(); string line; while (!streamReader.EndOfStream) { line = streamReader.ReadLine(); if (line == null) { /* * FIXME: report error?? */ break; } if (stateMap.ContainsKey(line)) { state = stateMap[line]; index = 0; continue; } if (state == -1) { if (Regex.IsMatch(line, @"[vV]ersion\ *")) { loaded.rom[RomAddress.Meta] = byte.Parse(Regex.Replace(line, @"[vV]ersion\ *", ""), NumberStyles.Integer); } } else if (state == stateMap["__lua__"]) { codeBuilder.AppendLine(line); } else if (state == stateMap["__gfx__"]) { foreach (var c in line) { var val = byte.Parse(c.ToString(), NumberStyles.HexNumber); Util.SetHalf(ref loaded.rom[index / 2 + RomAddress.Gfx], val, index % 2 == 0); index += 1; } } else if (state == stateMap["__gff__"]) { for (var i = 0; i < line.Length; i += 2) { loaded.rom[RomAddress.GfxProps + index] = byte.Parse(line.Substring(i, 2), NumberStyles.HexNumber); index += 1; } } else if (state == stateMap["__map__"]) { for (var i = 0; i < line.Length; i += 2) { loaded.rom[RomAddress.Map + index] = byte.Parse(line.Substring(i, 2), NumberStyles.HexNumber); index += 1; } } else if (state == stateMap["__sfx__"]) { if (Regex.IsMatch(line, @"^\s*$")) { continue; } var editor = byte.Parse(line.Substring(0, 2), NumberStyles.HexNumber); var speed = byte.Parse(line.Substring(2, 2), NumberStyles.HexNumber); var startLoop = byte.Parse(line.Substring(4, 2), NumberStyles.HexNumber); var endLoop = byte.Parse(line.Substring(6, 2), NumberStyles.HexNumber); loaded.rom[RomAddress.Sfx + index * 68 + 64] = editor; loaded.rom[RomAddress.Sfx + index * 68 + 65] = speed; loaded.rom[RomAddress.Sfx + index * 68 + 66] = startLoop; loaded.rom[RomAddress.Sfx + index * 68 + 67] = endLoop; var off = 0; for (var i = 0; i < line.Length - 8; i += 5) { var pitch = byte.Parse(line.Substring(i + 8, 2), NumberStyles.HexNumber); var waveform = byte.Parse(line.Substring(i + 8 + 2, 1), NumberStyles.HexNumber); var volume = byte.Parse(line.Substring(i + 8 + 3, 1), NumberStyles.HexNumber); var effect = byte.Parse(line.Substring(i + 8 + 4, 1), NumberStyles.HexNumber); var lo = (byte)(pitch | (waveform << 6)); var hi = (byte)((waveform >> 2) | (volume << 1) | (effect << 4)); loaded.rom[RomAddress.Sfx + index * 68 + off] = lo; loaded.rom[RomAddress.Sfx + index * 68 + off + 1] = hi; off += 2; } index += 1; } else if (state == stateMap["__music__"]) { if (Regex.IsMatch(line, @"^\s*$")) { continue; } var flag = byte.Parse(line.Substring(0, 2), NumberStyles.HexNumber); var val1 = byte.Parse(line.Substring(3, 2), NumberStyles.HexNumber); var val2 = byte.Parse(line.Substring(5, 2), NumberStyles.HexNumber); var val3 = byte.Parse(line.Substring(7, 2), NumberStyles.HexNumber); var val4 = byte.Parse(line.Substring(9, 2), NumberStyles.HexNumber); // 4th byte never has 7th bit set because it's corresponding flag value is never used. if ((flag & 0x1) != 0) { val1 |= 0x80; } if ((flag & 0x2) != 0) { val2 |= 0x80; } if ((flag & 0x4) != 0) { val3 |= 0x80; } loaded.rom[RomAddress.Song + index + 0] = val1; loaded.rom[RomAddress.Song + index + 1] = val2; loaded.rom[RomAddress.Song + index + 2] = val3; loaded.rom[RomAddress.Song + index + 3] = val4; index += 4; } } loaded.code = LuaPatcher.PatchCode(codeBuilder.ToString()); streamReader.Close(); return(true); }