int jass_set(lua_State* L) { if (!is_gaming()) { return 0; } const char* name = luaL_checkstring(L, 2); jass::global_variable gv(name); if (gv.is_vaild()) { if (!gv.is_array()) { gv = jass_read(L, jass::opcode_type_to_var_type(gv.type()), 3); } else { switch (gv.type()) { case jass::OPCODE_VARIABLE_INTEGER_ARRAY: case jass::OPCODE_VARIABLE_REAL_ARRAY: case jass::OPCODE_VARIABLE_STRING_ARRAY: case jass::OPCODE_VARIABLE_HANDLE_ARRAY: case jass::OPCODE_VARIABLE_BOOLEAN_ARRAY: break; default: break; } } } return 0; }
int jass_get(lua_State* L) { const char* name = lua_tostring(L, 2); jass::func_value const* nf = jass::jass_func(name); if (nf && nf->is_valid()) { if (!lua::allow_yield(L)) { if ((0 == strcmp(name, "TriggerSleepAction")) || (0 == strcmp(name, "TriggerWaitForSound")) || (0 == strcmp(name, "TriggerSyncReady")) || (0 == strcmp(name, "SyncSelections"))) { lua_pushstring(L, name); lua_pushcclosure(L, jass_call_null_function, 1); return 1; } } lua_pushinteger(L, (uint32_t)(uintptr_t)nf); lua_pushcclosure(L, jass_call_closure, 1); return 1; } if (!is_gaming()) { lua_pushnil(L); return 1; } jass::global_variable gv(name); if (gv.is_vaild()) { if (!gv.is_array()) { globals::jass_get_global_variable(L, gv.type(), gv); return 1; } else { switch (gv.type()) { case jass::OPCODE_VARIABLE_INTEGER_ARRAY: case jass::OPCODE_VARIABLE_REAL_ARRAY: case jass::OPCODE_VARIABLE_STRING_ARRAY: case jass::OPCODE_VARIABLE_HANDLE_ARRAY: case jass::OPCODE_VARIABLE_BOOLEAN_ARRAY: return jarray_create(L, (uintptr_t)gv.ptr()); default: lua_pushnil(L); return 1; } } } lua_pushnil(L); return 1; }
int jass_set(lua_State* L) { lua::jassbind* lj = (lua::jassbind*)L; if (!is_gaming()) { return 0; } const char* name = lj->tostring(2); hashtable::variable_node* ptr = get_variable_hashtable()->get(name); if (ptr) { switch (ptr->type_) { case jass::OPCODE_VARIABLE_NOTHING: case jass::OPCODE_VARIABLE_UNKNOWN: case jass::OPCODE_VARIABLE_NULL: break; case jass::OPCODE_VARIABLE_CODE: // // Fixed me // break; case jass::OPCODE_VARIABLE_INTEGER: ptr->value_ = lj->read_integer(3); break; case jass::OPCODE_VARIABLE_REAL: ptr->value_ = lj->read_real(3); break; case jass::OPCODE_VARIABLE_STRING: // // Fixed me // break; case jass::OPCODE_VARIABLE_HANDLE: ptr->value_ = lj->read_handle(3); break; case jass::OPCODE_VARIABLE_BOOLEAN: ptr->value_ = lj->read_boolean(3); break; break; case jass::OPCODE_VARIABLE_INTEGER_ARRAY: case jass::OPCODE_VARIABLE_REAL_ARRAY: case jass::OPCODE_VARIABLE_STRING_ARRAY: case jass::OPCODE_VARIABLE_HANDLE_ARRAY: case jass::OPCODE_VARIABLE_BOOLEAN_ARRAY: break; default: break; } } return 0; }
int fix_math_randomseed(lua_State* L) { if (is_gaming()) { jass::call("SetRandomSeed", (unsigned int)(lua_Integer)luaL_checknumber(L, 1)); } else { srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1)); (void)rand(); } return 0; }
int jass_get(lua_State* L) { const char* name = lua_tostring(L, 2); jass::func_value const* nf = jass::jass_func(name); if (nf && nf->is_valid()) { lua_pushinteger(L, (uint32_t)(uintptr_t)nf); lua_pushcclosure(L, jass_call_closure, 1); return 1; } if (!is_gaming()) { lua_pushnil(L); return 1; } jass::global_variable gv(name); if (gv.is_vaild()) { if (!gv.is_array()) { globals::jass_get_global_variable(L, gv.type(), gv); return 1; } else { switch (gv.type()) { case jass::OPCODE_VARIABLE_INTEGER_ARRAY: case jass::OPCODE_VARIABLE_REAL_ARRAY: case jass::OPCODE_VARIABLE_STRING_ARRAY: case jass::OPCODE_VARIABLE_HANDLE_ARRAY: case jass::OPCODE_VARIABLE_BOOLEAN_ARRAY: return jarray_create(L, (uintptr_t)gv.ptr()); default: lua_pushnil(L); return 1; } } } lua_pushnil(L); return 1; }
int jass_call_closure(lua_State* L) { if (!is_gaming()) { lua_pushnil(L); return 1; } int result = jass_call_native_function(L, (const jass::func_value*)lua_tointeger(L, lua_upvalueindex(1))); if (lua_isyieldable(L)) { jass_vm_t* thread = get_jass_thread(); if (thread && thread->has_sleep) { thread->opcode -= jass::trampoline_size() / sizeof(jass::opcode); return lua_yield(L, 0); } } return result; }
int fix_math_random(lua_State* L) { lua_Integer low = 0, up = 1; double r; if (is_gaming()) { jass::jreal_t a = jass::to_real(0.f); jass::jreal_t b = jass::to_real(1.f); r = (double)jass::from_real(jass::call("GetRandomReal", &a, &b)); } else { r = (double)rand() * (1.0 / ((double)RAND_MAX + 1.0)); } switch (lua_gettop(L)) { case 0: lua_pushnumber(L, (lua_Number)r); break; case 1: low = 1; up = luaL_checkinteger(L, 1); break; case 2: low = luaL_checkinteger(L, 1); up = luaL_checkinteger(L, 2); break; default: return luaL_error(L, "wrong number of arguments"); } luaL_argcheck(L, low <= up, 1, "interval is empty"); luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1, "interval too large"); r *= (double)(up - low) + 1.0; lua_pushinteger(L, (lua_Integer)r + low); return 1; }
int jass_call_closure(lua_State* L) { lua::jassbind* lj = (lua::jassbind*)L; if (!is_gaming()) { lj->pushnil(); return 1; } native_function::native_function* nf = (native_function::native_function*)lj->tounsigned(lua_upvalueindex(1)); size_t param = nf->get_param().size(); if ((int)param > lj->gettop()) { lj->pushnil(); return 1; } std::unique_ptr<uintptr_t[]> buffer(new uintptr_t[param]); std::vector<jass::jreal_t> real_buffer(param); std::vector<std::unique_ptr<jass::string_fake>> string_buffer(param); for (size_t i = 0; i < param; ++i) { native_function::variable_type vt = nf->get_param()[i]; switch (vt) { case native_function::TYPE_BOOLEAN: buffer[i] = lj->read_boolean(i+1); break; case native_function::TYPE_CODE: buffer[i] = (jass::jcode_t)util::singleton_nonthreadsafe<jump_func>::instance().create(lua::callback(lj, i+1), 'YDWE'); break; case native_function::TYPE_HANDLE: buffer[i] = lj->read_handle(i+1); break; case native_function::TYPE_INTEGER: buffer[i] = lj->read_integer(i+1); break; case native_function::TYPE_REAL: real_buffer[i] = lj->read_real(i+1); buffer[i] = (uintptr_t)&real_buffer[i]; break; case native_function::TYPE_STRING: string_buffer[i].reset(new jass::string_fake(lj->tostring(i+1))); buffer[i] = (jass::jstring_t)*string_buffer[i]; break; default: assert(false); buffer[i] = 0; break; } } uintptr_t retval = nf->call(buffer.get()); switch (nf->get_return()) { case native_function::TYPE_NOTHING: return 0; case native_function::TYPE_BOOLEAN: lj->push_boolean(retval); return 1; case native_function::TYPE_CODE: lj->push_code(retval); return 1; case native_function::TYPE_HANDLE: lj->push_handle(retval); return 1; case native_function::TYPE_INTEGER: lj->push_integer(retval); return 1; case native_function::TYPE_REAL: lj->push_real(retval); return 1; case native_function::TYPE_STRING: lj->push_string(retval); return 1; default: assert(false); break; } return 0; }
int jass_get(lua_State* L) { lua::jassbind* lj = (lua::jassbind*)L; const char* name = lj->tostring(2); native_function::native_function const* nf = native_function::jass_func(name); if (nf) { lj->pushunsigned((uint32_t)(uintptr_t)nf); lj->pushcclosure((lua::state::cfunction)jass_call_closure, 1); return 1; } if (!is_gaming()) { lj->pushnil(); return 1; } hashtable::variable_node* node_ptr = get_variable_hashtable()->get(name); if (node_ptr) { switch (node_ptr->type_) { case jass::OPCODE_VARIABLE_NOTHING: case jass::OPCODE_VARIABLE_UNKNOWN: case jass::OPCODE_VARIABLE_NULL: lj->pushnil(); return 1; case jass::OPCODE_VARIABLE_CODE: lj->push_code(node_ptr->value_); return 1; case jass::OPCODE_VARIABLE_INTEGER: lj->push_integer(node_ptr->value_); return 1; case jass::OPCODE_VARIABLE_REAL: lj->push_real(node_ptr->value_); return 1; case jass::OPCODE_VARIABLE_STRING: lj->push_string(node_ptr->value_); return 1; case jass::OPCODE_VARIABLE_HANDLE: lj->push_handle(node_ptr->value_); return 1; case jass::OPCODE_VARIABLE_BOOLEAN: lj->push_boolean(node_ptr->value_); return 1; case jass::OPCODE_VARIABLE_INTEGER_ARRAY: case jass::OPCODE_VARIABLE_REAL_ARRAY: case jass::OPCODE_VARIABLE_STRING_ARRAY: case jass::OPCODE_VARIABLE_HANDLE_ARRAY: case jass::OPCODE_VARIABLE_BOOLEAN_ARRAY: // // Fixed me // lj->pushnil(); return 1; default: return 1; } } lj->pushnil(); return 1; }