跳到主要内容

util_lib

简介

一些工具函数和工具组件的集合

组件接口

FEntry0.gs

被包含FLoadEntries0组件的对象管理的子对象,可选择包含本组件以便获得额外的功能(比如通过update更新后可以自动向管理对象注册自己)

函数原型函数作用
string get_entry_id()获取用于注册的id
void register_myself()注册自己
object query_mgr(string path)获取自己的管理器对象

FLoadEntries0.gs

用于管理子对象的组件

函数原型函数作用
void claim_dir(string path, array load_args)声明自己管理了某个目录(记录本管理器管理了某个目录)
void reload()重新注册和加载所有接口对象
void clear_all_entry()清空并销毁所有已登记的接口对象
void clear_all_dirs()清空本管理器记录的所有管理目录
void load_all_entry(string path, mixed para = nil, domain d = nil, bool recursive = false)加载并且注册指定目录下的所有接口对象
void load_entry(mixed entry_id, object ob)注册一个接口对象
mixed query_entry(mixed entry_id)查询一个接口对象
map query_all_entries()查询所有接口对象
array get_all_dirs()获取本管理器管理的所有目录
void remove_entry(mixed entry_id)移除一个接口对象

util_directory.gs

文件目录相关的一些功能函数集合

函数原型函数作用
string get_os_path(string script_path)获取操作系统路径
string get_script_path(string os_path)获取脚本路径
bool is_same_script_path(string path1, string path2)判断两个脚本路径指向的系统路径是否相同
string get_relative_path(string path1, string path2)获取两个路径的相对路径
string get_cwd()获取当前工作目录
bool move_file(string src_file, string to_file)移动文件

util_function.gs

function 相关的一些功能函数集合

函数原型函数作用
array function.param_info(function func)获取函数实例的形参信息

util_map_array.gs

map, array 相关的一些功能函数集合

函数原型函数作用
int array.search(array arr, function func)查找数组中符合条件的第一个元素的索引
array array.distinct(array arr)数组去重(直接修改原数组)
bool array.deep_compare(array left, array right)深度比较两个数组
bool array.deep_contains(array arr, mixed val)深度判断数组是否包含指定值
array array.deep_merge(array left, array right, bool replace = true)深度合并两个数组(相同位置元素合并)
array array.deep_remove(array left, array right)从数组1中移除所有被数组2包含的元素
bool map.deep_compare(map left, map right)深度比较两个 map
map map.deep_absorb(map left, map right, bool replace = true)深度合并两个词典
map map.deep_remove(map left, map right)深度移除词典1中所有被词典2包含的键值对

util_math.gs

一些数学功能函数集合

函数原型函数作用
bool is_number(mixed val)判断是否为数字(包括整数和浮点数)
mixed math.sum(array arr)求数组中的元素和(推荐用array.sum)
mixed vector3_dot(map a, map b)计算向量3D点积
float vec2_magnitude_distance(float x1, float y1, float x2, float y2)计算两个二维点之间的距离
float pos2_magnitude_distance(map pos1, map pos2)两个坐标点距离的平方
float vec2_distance(float x1, float y1, float x2, float y2)两个坐标点距离
float pos2_distance(map pos1, map pos2)两个2D坐标点距离
bool pos2_dist_lessthan(map pos1, map pos2, mixed dist)两个2D坐标点距离在指定距离内
map vec2_lerp(map pos_begin, map pos_end, float lerp_val)二维坐标线性插值
map vec3_lerp(map pos_begin, map pos_end, float lerp_val)3维坐标线性插值
map pos3(mixed x, mixed y, mixed z)新建一个3D坐标实例
map pos2(mixed x, mixed z)新建一个2D坐标实例
int random_one()随机生成一个1或-1
float math.lerp(mixed from, mixed to, float val)线性插值
float math.random_real(float from, float to)随机生成一个区间内的浮点数
float math.random_one()随机生成一个(0~1)的浮点数
mixed math.shuffle_select(array arr)从数组中随机选择一个元素
float math.boxmuller(float mu = 0, float sigma = 1, int rand_max = 1000000)返回一个服从正态分布的随机数(mu表示均值, sigma表示标准差, rand_max表示分布概率)
float pos_distance(map a, map b)计算两个2D坐标距离
float pos_distance_pow(map a, map b)计算两个2D坐标距离的平方
float world_pos_distance(map a, map b)计算两个3D坐标距离
float world_pos_distance_pow(map a, map b)计算两个3D坐标距离的平方
int grid_pos_manhattan_distance(map a, map b)计算两个2D坐标的曼哈顿距离

util_misc.gs

一些杂项功能函数集合

函数原型函数作用
string new_guid()产生一个GUID
int adler32(string text)adler32算法
int create_check_sum(int a, string b)计算一个基于adler32算法的校验码
int new_cookie()产生一个当前进程内唯一的int值
array box_msgpackable_value(mixed v)将一个指定值装箱为可以被msgpack.pack打包的数组
mixed unbox_msgpackable_value(array arr)将由box_msgpackable_value打包的数组还原为值
int combine_int64(int int32_value1, int int32_value2)将两个32位整数组合成一个64位整数
array split_int64(int int64_value)将一个64位整数拆分成两个32位整数
int fetch_program_file_crc64(string program_file)根据代码文件路径获取文件CRC64校验和
bool program.is_self_out_of_date(program p)判断program自身是否过期
bool program.is_out_of_date(program p, array program_file_list = nil)判断program是否过期
bool program.is_components_out_of_date(program p, array program_file_list = nil)判断组件的program是否过期

util_object.gs

object相关的一些功能函数集合

函数原型函数作用
object safe_load_object(string f, mixed para = nil, domain d = nil)安全载入一个静态对象
void safe_load_directory(string path, mixed para = , domain d = nil)安全加载目录下的所有静态对象
array find_scripts(string path, bool recursively = false)获取指定目录下的所有脚本文件(以.gs或者.o结尾的文件,目录路径支持 * 作为通配符)
domain assure_domain(mixed d)确保一个域有效
domain create_domain(string domain_name)创建一个指定名称的域
void assure_in_domain(object ob)断言当前域就是对象的域
string file2domain(string file_name, string identifier = "DOMAIN")为一个文件名生成一个格式化的域名
string get_pure_name(string obfile)获取一个文件名中不带路径和扩展名的纯文件名
void load_static_objects_in(string path)加载目录下所有未加载的脚本(不处理子目录)
void destruct_static_objects_in(string path)销毁目录下所有已加载的静态脚本对象(不处理子目录)
void compile_scripts_in(string path = "/", bool recursively = true, bool force_recompile = false)调试接口 - 编译指定路径下的脚本

util_random.gs

一些随机功能函数集合

函数原型函数作用
map random_select_by_weight(array arr, string weight_key = "weight")根据权重表随机选择,所有权重为负数的都变成0
array random_sample(int n, int m)随机采样
array random_sample_arr(array arr, int m)从数组中随机采样m个数据,保证每个选中的概率为m/n

util_value.gs

类型相关的一些功能函数集合

函数原型函数作用
bool is_domain(mixed h)是否为domain类型
bool is_timer(mixed h)是否为timer类型
bool is_coroutine(mixed h)是否为coroutine类型
bool is_sync_object(mixed h)是否为sync_object类型
bool is_queue(mixed h)是否为queue类型
bool is_socket(mixed h)是否为socket类型
bool is_share_value(mixed h)是否为share_value类型
bool is_share_map(mixed h)是否为share_map类型(share_value创建时指定为map)
bool is_share_array(mixed h)是否为share_array类型(share_value创建时指定为array)
bool is_share_int(mixed h)是否为share_int类型(share_value创建时指定为int)
bool is_number(mixed val)是否为数值类型(int 或者 float类型)
mixed maximum(...)求最大值
mixed minimum(...)求最小值
bool is_equal_value(mixed v1, mixed v2)比较两个值是否相等;特别的,对MAP和ARRAY进行递归按值比较