public LuaArgs searchpath(LuaArgs args) { var name = args.GetString(0); var path = args.GetString(1); var sep = args.IsNil(2) ? "." : args.GetString(2); var rep = args.IsNil(3) ? "/" : args.GetString(3); var fixedName = name.Replace(sep, rep); var fixedPath = path.Replace("?", fixedName); var candidates = fixedPath.Split(';'); var errorBuilder = new StringBuilder(); foreach (var candidate in candidates) { var candPath = new FilePath(candidate); if (m_fileSystem.Exists(candPath) && !m_fileSystem.IsDir(candPath)) { return(new LuaArgs(candidate)); } else { if (errorBuilder.Length > 0) { errorBuilder.Append('\n'); } errorBuilder.Append("no file '" + candidate + "'"); } } return(new LuaArgs(LuaValue.Nil, errorBuilder.ToString())); }
public LuaArgs playMusic(LuaArgs args) { var path = args.IsNil(0) ? null : args.GetString(0); var loop = args.IsNil(1) ? true : args.GetBool(1); var transition = args.IsNil(2) ? 1.0f : args.GetFloat(2); if (transition < 0.0f) { throw new LuaError("Transition duration must be positive"); } m_state.PlayMusic(path, transition, loop); return(LuaArgs.Empty); }
public LuaArgs seek(LuaArgs args) { try { var whence = args.IsNil(0) ? "cur" : args.GetString(0); var offset = args.IsNil(1) ? 0 : args.GetInt(1); CheckOpen(); if (m_contentType == LuaFileContentType.Binary) { // Seek in a binary file switch (whence) { case "set": { m_stream.Seek(offset, SeekOrigin.Begin); break; } case "cur": { m_stream.Seek(offset, SeekOrigin.Current); break; } case "end": { m_stream.Seek(offset, SeekOrigin.End); break; } default: { throw new LuaError("Unsupported option: " + whence); } } return(new LuaArgs(m_stream.Position)); } else { // Seek in a text file throw new NotImplementedException(); } } catch (IOException e) { return(new LuaArgs(LuaValue.Nil, e.Message)); } }
public LuaArgs newPalette(LuaArgs args) { var size = args.IsNil(0) ? 16 : args.GetInt(0); if (size < 1 || size > 256) { throw new LuaError("Palette size must be in range 1-256"); } if (m_computer.Memory.Alloc(size * 3)) { // Create a simple greyscale palette uint usize = (uint)size; uint[] colors = new uint[usize]; colors[0] = 0x000000ff; for (uint i = 1; i < colors.Length; ++i) { uint bright = ((i * 255) / (usize - 1)) & 0xff; colors[i] = (bright << 24) + (bright << 16) + (bright << 8) + 255; } var palette = new Palette(colors); return(new LuaArgs(new LuaPalette(palette, m_computer.Memory))); } else { throw new LuaError("not enough memory"); } }
public LuaArgs saveTGA(LuaArgs args) { var image = args.GetObject <LuaImage>(0).Image; var palette = args.GetObject <LuaPalette>(1).Palette; try { if (args.IsNil(2)) { // Save to a string and return it using (var stream = new MemoryStream()) { TGAImage.Encode(image, palette, stream); return(new LuaArgs(stream.ToArray())); } } else { // Save to a stream var file = args.GetObject <LuaFile>(2); CheckFileMode(file, "tga", LuaFileOpenMode.Write, LuaFileContentType.Binary); TGAImage.Encode(image, palette, file.InnerStream); return(LuaArgs.Empty); } } catch (IOException e) { throw new LuaError(e.Message); } }
public LuaArgs mount(LuaArgs args) { try { var mount = args.GetObject <LuaMount>(0); var path = new FilePath(args.GetString(1)); var subPath = args.IsNil(2) ? FilePath.Empty : new FilePath(args.GetString(2)); var readOnly = args.IsNil(3) ? false : args.GetBool(3); m_fileSystem.Mount(mount, path, subPath, readOnly); return(LuaArgs.Empty); } catch (IOException e) { throw new LuaError(e.Message); } }
public LuaArgs open(LuaArgs args) { var path = Sanitize(args.GetString(0)); var mode = args.IsNil(1) ? "r" : args.GetString(1); try { CheckConnected(); if (mode == "r") { CheckFileExists(path); return(new LuaArgs(new LuaFile(Mount.OpenForRead(path)))); } else if (mode == "rb") { CheckFileExists(path); return(new LuaArgs(new LuaFile(Mount.OpenForBinaryRead(path), LuaFileOpenMode.Read))); } else if (mode == "w") { CheckWritable(); CheckDirDoesNotExist(path); CheckParentDirExists(path); return(new LuaArgs(new LuaFile(WritableMount.OpenForWrite(path, false)))); } else if (mode == "wb") { CheckWritable(); CheckDirDoesNotExist(path); CheckParentDirExists(path); return(new LuaArgs(new LuaFile(WritableMount.OpenForBinaryWrite(path, false), LuaFileOpenMode.Write))); } else if (mode == "a") { CheckWritable(); CheckDirDoesNotExist(path); CheckParentDirExists(path); return(new LuaArgs(new LuaFile(WritableMount.OpenForWrite(path, true)))); } else if (mode == "ab") { CheckWritable(); CheckDirDoesNotExist(path); CheckParentDirExists(path); return(new LuaArgs(new LuaFile(WritableMount.OpenForBinaryWrite(path, true), LuaFileOpenMode.Write))); } else { throw new LuaError("Unsupported mode: " + mode); } } catch (FileNotFoundException) { return(LuaArgs.Nil); } catch (IOException e) { throw new LuaError(e.Message); } }
public LuaArgs read(LuaArgs args) { int x = args.GetInt(0); int y = args.GetInt(1); if (args.IsNil(2)) { if (x >= 0 && x < Image.Width && y >= 0 && y < Image.Height) { var b = Image[x, y]; return(new LuaArgs(b)); } return(new LuaArgs(0)); } else { int length = args.GetInt(2); if (length < 0) { throw new LuaError("Read length must be positive"); } if (x < 0 || x >= Image.Width || y < 0 || y >= Image.Height || (x + y * Image.Width + length) > Image.Width * Image.Height) { throw new LuaError("Read must start and end within the image bounds"); } var result = Image.Read(x, y, length); return(new LuaArgs(result)); } }
public LuaArgs clear(LuaArgs args) { var color = args.IsNil(0) ? (byte)0 : args.GetByte(0); m_graphics.Clear(color); return(LuaArgs.Empty); }
public LuaArgs fill(LuaArgs args) { var color = args.GetByte(0); if (args.IsNil(1)) { CheckWritable(); Image.Fill(color); } else { int x = args.GetInt(1); int y = args.GetInt(2); int w = args.GetInt(3); int h = args.GetInt(4); if (w < 0) { throw new LuaError("Fill width must be positive"); } if (h < 0) { throw new LuaError("Fill height must be positive"); } if (x < 0 || x + w > Image.Width || y < 0 || y + h > Image.Height) { throw new LuaError("Fill must start and end within the image bounds"); } CheckWritable(); Image.Fill(color, x, y, w, h); } return(LuaArgs.Empty); }
public LuaArgs read(LuaArgs args) { int start = args.GetInt(0); if (args.IsNil(1)) { if (start >= 0 && start < Buffer.Length) { var b = Buffer[start]; return(new LuaArgs(b)); } return(new LuaArgs(0)); } else { int length = args.GetInt(1); if (length < 0) { throw new LuaError("Read length must be positive"); } if (start < 0 || start + length > Buffer.Length) { throw new LuaError("Read must start and end within the buffer"); } var result = Buffer.Read(start, length); return(new LuaArgs(result)); } }
public LuaArgs output(LuaArgs args) { if (args.IsNil(0)) { return(new LuaArgs(m_output.Value)); } else if (args.IsObject <LuaFile>(0)) { m_output.Value = args.GetObject <LuaFile>(0); return(LuaArgs.Empty); } else { var path = new FilePath(args.GetString(0)); try { m_output.Value = new LuaFile(m_fileSystem.OpenForWrite(path, false)); return(LuaArgs.Empty); } catch (IOException e) { throw new LuaError(e.Message); } } }
public LuaArgs write(LuaArgs args) { try { CheckOpen(); if (m_openMode == LuaFileOpenMode.Write) { // Write some stuff int i = 0; LuaContinuation continuation = null; continuation = delegate { if (!args.IsNil(i)) { return(WriteOneAsync(args[i++], continuation)); } else { return(new LuaArgs(this)); } }; return(continuation.Invoke(LuaArgs.Empty)); } return(new LuaArgs(this)); } catch (IOException e) { return(new LuaArgs(LuaValue.Nil, e.Message)); } }
public LuaArgs startParticles(LuaArgs args) { var path = args.GetString(0); var startActive = args.IsNil(1) ? false : args.GetBool(1); m_entity.StartParticles(path, startActive); return(LuaArgs.Empty); }
public LuaArgs playSound(LuaArgs args) { var path = args.GetString(0); var loop = args.IsNil(1) ? false : args.GetBool(1); m_entity.PlaySound(path, loop); return(LuaArgs.Empty); }
public LuaArgs popen(LuaArgs args) { args.GetString(0); if (!args.IsNil(1)) { args.GetString(1); } return(LuaArgs.Nil); }
public LuaArgs showDialogue(LuaArgs args) { var character = args.GetString(0); var dialogue = args.GetString(1); var modal = args.IsNil(2) ? true : args.GetBool(2); m_state.ShowDialogue(character, dialogue, modal); return(LuaArgs.Empty); }
public LuaArgs translate(LuaArgs args) { var x = args.GetInt(0); var y = args.GetInt(1); var fillColor = args.IsNil(2) ? (byte)0 : args.GetByte(2); CheckWritable(); Image.Translate(x, y, fillColor); return(LuaArgs.Empty); }
public LuaArgs setlocale(LuaArgs args) { if (args.IsNil(0)) { if (!args.IsNil(1)) { args.GetString(1); } return(new LuaArgs("C")); } else { args.GetString(0); if (!args.IsNil(1)) { args.GetString(1); } return(LuaArgs.Empty); } }
public LuaArgs setTransparentColor(LuaArgs args) { byte?color = null; if (!args.IsNil(0)) { color = args.GetByte(0); } m_graphics.TransparentColor = color; return(LuaArgs.Empty); }
public LuaArgs exit(LuaArgs args) { if (!args.IsNil(0) && !args.IsBool(0)) { args.GetInt(0); } m_cpu.RequestShutdown(); throw new LuaYield(LuaArgs.Empty, delegate { return(LuaArgs.Empty); }); }
public LuaArgs stopMusic(LuaArgs args) { var transition = args.IsNil(0) ? 1.0f : args.GetFloat(1); if (transition < 0.0f) { throw new LuaError("Transition duration must be positive"); } m_state.PlayMusic(null, transition, false); return(LuaArgs.Empty); }
public LuaArgs close(LuaArgs args) { if (args.IsNil(0)) { return(m_output.Value.close(LuaArgs.Empty)); } else { var file = args.GetObject <LuaFile>(0); return(file.close(LuaArgs.Empty)); } }
public LuaArgs stop(LuaArgs args) { if (args.IsNil(0)) { m_speaker.Stop(); } else { int channel = args.GetInt(0); m_speaker.Stop(channel); } return(LuaArgs.Empty); }
public LuaArgs execute(LuaArgs args) { var command = args.IsNil(0) ? null : args.GetString(0); if (command != null) { return(LuaArgs.Nil); } else { return(new LuaArgs(false)); } }
public LuaArgs xorImage(LuaArgs args) { int startX = args.GetInt(0); int startY = args.GetInt(1); var image = args.GetObject <LuaImage>(2).Image; int scale = args.IsNil(3) ? 1 : args.GetInt(3); if (scale <= 0) { throw new LuaError("Scale must be an integer 1 or greater"); } m_graphics.XorImage(startX, startY, image, scale); return(LuaArgs.Empty); }
public LuaArgs drawMap(LuaArgs args) { int startX = args.GetInt(0); int startY = args.GetInt(1); var map = args.GetObject <LuaImage>(2).Image; var tileset = args.GetObject <LuaImage>(3).Image; int scale = args.IsNil(4) ? 1 : args.GetInt(4); if (scale <= 0) { throw new LuaError("Scale must be an integer 1 or greater"); } m_graphics.DrawMap(startX, startY, map, tileset, scale); return(LuaArgs.Empty); }
public LuaArgs getDeviceNames(LuaArgs args) { var filter = args.IsNil(0) ? null : args.GetString(0); var devices = m_computer.Devices; var table = new LuaTable(devices.Count); int i = 1; foreach (var device in devices) { if (filter == null || device.Type == filter) { table[i++] = devices.GetName(device); } } return(new LuaArgs(table)); }
public LuaArgs playAnimation(LuaArgs args) { var path = args.GetString(0); var animateRoot = args.IsNil(1) ? true : args.GetBool(1); if (LuaAnimation.Exists(path)) { var anim = LuaAnimation.Get(path); m_entity.StartAnimation(anim, animateRoot); return(LuaArgs.Empty); } else { throw new LuaError(string.Format("No such animation: {0}", path)); } }
public LuaArgs setFont(LuaArgs args) { var font = args.IsNil(0) ? null : args.GetObject <LuaFont>(0); if (font != null) { m_graphics.Font = font.Font; m_font.Value = font; } else { m_graphics.Font = null; m_font.Value = null; } return(LuaArgs.Empty); }