2008-11-07 08:45:18 +00:00
|
|
|
/*
|
|
|
|
Monster - an advanced game scripting language
|
|
|
|
Copyright (C) 2007, 2008 Nicolay Korslund
|
|
|
|
Email: <korslund@gmail.com>
|
|
|
|
WWW: http://monster.snaptoad.com/
|
|
|
|
|
|
|
|
This file (vm.d) is part of the Monster script language package.
|
|
|
|
|
|
|
|
Monster is distributed as free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License
|
|
|
|
version 3, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
version 3 along with this program. If not, see
|
|
|
|
http://www.gnu.org/licenses/ .
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
module monster.vm.vm;
|
|
|
|
|
2008-12-30 01:59:24 +00:00
|
|
|
import monster.vm.error;
|
|
|
|
import monster.vm.fstack;
|
|
|
|
import monster.vm.thread;
|
|
|
|
import monster.vm.mclass;
|
|
|
|
import monster.vm.mobject;
|
2008-11-07 08:45:18 +00:00
|
|
|
|
2008-12-30 01:59:24 +00:00
|
|
|
import monster.compiler.tokenizer;
|
2008-11-07 08:45:18 +00:00
|
|
|
import monster.compiler.linespec;
|
|
|
|
import monster.compiler.functions;
|
2008-12-30 01:59:24 +00:00
|
|
|
import monster.compiler.assembler;
|
2008-11-07 08:45:18 +00:00
|
|
|
import monster.compiler.scopes;
|
|
|
|
|
2009-01-14 20:51:17 +00:00
|
|
|
import monster.modules.timer;
|
2009-01-16 12:56:54 +00:00
|
|
|
import monster.modules.frames;
|
2009-01-14 20:51:17 +00:00
|
|
|
|
2008-12-30 01:59:24 +00:00
|
|
|
import std.file;
|
|
|
|
import monster.util.string;
|
2008-11-07 08:45:18 +00:00
|
|
|
|
2008-12-30 01:59:24 +00:00
|
|
|
VM vm;
|
2008-11-07 08:45:18 +00:00
|
|
|
|
2008-12-30 01:59:24 +00:00
|
|
|
struct VM
|
2008-11-07 08:45:18 +00:00
|
|
|
{
|
2008-12-30 01:59:24 +00:00
|
|
|
// Run a script file in the context of the object obj. If no object
|
|
|
|
// is given, an instance of an empty class is used.
|
|
|
|
void run(char[] file, MonsterObject *obj = null)
|
2008-11-07 08:45:18 +00:00
|
|
|
{
|
2008-12-30 01:59:24 +00:00
|
|
|
if(obj !is null)
|
2008-11-07 08:45:18 +00:00
|
|
|
{
|
2008-12-30 01:59:24 +00:00
|
|
|
auto func = Function(file, obj.cls);
|
|
|
|
func.call(obj);
|
2008-11-07 08:45:18 +00:00
|
|
|
}
|
2008-12-30 01:59:24 +00:00
|
|
|
else
|
2008-11-07 08:45:18 +00:00
|
|
|
{
|
2008-12-30 01:59:24 +00:00
|
|
|
auto func = Function(file);
|
|
|
|
func.call();
|
2008-11-07 08:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-14 20:51:17 +00:00
|
|
|
void frame(float time = 0)
|
|
|
|
{
|
|
|
|
if(time != 0)
|
|
|
|
idleTime.add(time);
|
|
|
|
|
2009-01-16 12:56:54 +00:00
|
|
|
updateFrames(time);
|
|
|
|
|
2009-01-14 20:51:17 +00:00
|
|
|
scheduler.doFrame();
|
|
|
|
}
|
|
|
|
|
2008-12-30 01:59:24 +00:00
|
|
|
// Path to search for script files. Extremely simple at the moment.
|
|
|
|
private char[][] includes = [""];
|
2008-11-07 08:45:18 +00:00
|
|
|
|
2008-12-30 01:59:24 +00:00
|
|
|
void addPath(char[] path)
|
2008-11-07 08:45:18 +00:00
|
|
|
{
|
2008-12-30 01:59:24 +00:00
|
|
|
// Make sure the path is slash terminated.
|
|
|
|
if(!path.ends("/") && !path.ends("\\"))
|
|
|
|
path ~= '/';
|
2008-11-07 08:45:18 +00:00
|
|
|
|
2008-12-30 01:59:24 +00:00
|
|
|
includes ~= path;
|
2008-11-07 08:45:18 +00:00
|
|
|
}
|
|
|
|
|
2008-12-30 01:59:24 +00:00
|
|
|
// Search for a file in the various paths. Returns true if found,
|
|
|
|
// false otherwise. Changes fname to point to the correct path.
|
|
|
|
bool findFile(ref char[] fname)
|
2008-11-07 08:45:18 +00:00
|
|
|
{
|
2008-12-30 01:59:24 +00:00
|
|
|
// Check against our include paths. In the future we will replace
|
|
|
|
// this with a more flexible system, allowing virtual file systems,
|
|
|
|
// archive files, complete platform independence, improved error
|
|
|
|
// checking etc.
|
|
|
|
foreach(path; includes)
|
2008-11-07 08:45:18 +00:00
|
|
|
{
|
2008-12-30 01:59:24 +00:00
|
|
|
char[] res = path ~ fname;
|
|
|
|
if(exists(res))
|
|
|
|
{
|
|
|
|
fname = res;
|
|
|
|
return true;
|
|
|
|
}
|
2008-11-07 08:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|