main.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace loyalsoft;
  3. header('Cache-Control: no-store'); # 防御缓存设施
  4. header('Host:' . PHP_OS . "-" . (isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : "")); # 这里提取服务器地址返给客户端,方便留意负载均衡/弹性扩容的效果
  5. header('PoweredBy: PHP-' . PHP_VERSION);
  6. $GLOBALS['OS'] = "win32"; # os, used by JsonUtil::encode()
  7. defined("ROOTDIR") or define("ROOTDIR", __DIR__); # 定义项目根目录
  8. /**
  9. * 获取项目根路径URL(限制条件,本方法所在的文件也必须在项目根路径下wg)
  10. * @author gwang
  11. * @return string
  12. */
  13. function getRootURL() {
  14. $self = substr(__FILE__, strlen($_SERVER['DOCUMENT_ROOT']));
  15. $self = str_replace('\\', '/', $self);
  16. $uri = substr($self, 0, strrpos($self, '/') + 1);
  17. $url = "http://" . $_SERVER['HTTP_HOST'] . $uri;
  18. return $url;
  19. }
  20. /**
  21. * 一种优化方案, 如果部署环境可以支持动态代码生成,将会减少与redis网络通信的次数.
  22. * (在云服务器实验中,开了不如不开效率高 ̄□ ̄||,而且文件模式需要在多台机子中分发.-gwang2017)
  23. * 经验证,复杂逻辑(会多次读取配置信息时)可以有效改善性能. -gwang 2020.11.24
  24. * @var boolean (部署环境)是否允许代码生成
  25. */
  26. define('CodeGen_Enabled', false)
  27. and CodeGen_Enabled
  28. and define('CodeGen_Folder', ROOTDIR . '/../../CodeGen/'); # 代码生成功能的输出位置
  29. require_once ROOTDIR . '/AutoLoad.php'; # 初始化框架自动加载机制
  30. /**
  31. * get dao instance,
  32. * @version 2019年12月23日 经过梦幻星工场2一年多的使用, 未发现特别明显的bug. 决定侧重使用. -- 王刚
  33. * 2017.06.23 第一版 学习自禅道的开源框架. -- 王刚
  34. * @staticvar type $a
  35. * @return \loyalsoft\dao 注意: dao并非什么好东西,只是一些改进尝试, 稳定性, 性能, 效率尚未得到证明.-gwang 2017.06.23
  36. */
  37. function daoInst() {
  38. static $a = null;
  39. if (is_null($a)) {
  40. $a = new dao(); # 结束请求的时候自动回收,无需主动写关闭代码
  41. $a->connectDB(config::Inst()); # 建立链接,传入配置文件
  42. }
  43. return $a;
  44. }
  45. /**
  46. * @return \loyalsoft\CRedisUtil 获取全局MEM单例
  47. */
  48. function gMem() {
  49. static $cmem = null;
  50. if ($cmem == null) {
  51. $nosql = config::Inst()->nosql;
  52. // $use_ext = true; # 使用扩展,测试用(暂时仅支持php7)
  53. // if ($use_ext) {
  54. // $cmem = new \Redis(); # 还是需要重写下, 之前的CRedisUtil已经增加了json_encode/decode.
  55. // $cmem->connect($nosql->host, $nosql->port);
  56. // $cmem->auth($nosql->pwd);
  57. // } else {
  58. $cmem = new CRedisUtil();
  59. $cmem->conn($nosql->host, $nosql->port, $nosql->pwd, $nosql->db);
  60. // }
  61. }
  62. return $cmem;
  63. }
  64. /**
  65. * MongoUtil 辅助类
  66. * @return MongoUtil
  67. */
  68. function gMongo() {
  69. static $mu = null;
  70. if (null == $mu) {
  71. $mu = new MongoUtil();
  72. $mc = config::Inst()->mongo;
  73. $mgr = $mu->conn($mc->conn, $mc->db);
  74. if ($mgr === false) {
  75. CLog::err("MongoDB连接异常!");
  76. }
  77. }
  78. return $mu;
  79. }
  80. /**
  81. * 简化提取全局变量的写法
  82. * @return \globalsettings
  83. */
  84. function glc() {
  85. return GameConfig::globalsettings();
  86. }
  87. /**
  88. * @return \loyalsoft\Req 全局访问
  89. */
  90. function req() {
  91. return Req::Ins();
  92. }
  93. /**
  94. * @param type $game 用参数更新 ctx
  95. * @return \loyalsoft\Data_UserGame 玩家数据
  96. */
  97. function ctx($game = null) {
  98. if (null != $game) {
  99. req()->game = $game;
  100. }
  101. return req()->game;
  102. }
  103. default_timezone(); # 全局范围内启用默认时区 中国时区
  104. $zoneid = "1"; # 定义全局变量zoneid
  105. /**
  106. * 从QueryString初始化分区Id
  107. * @global int $zoneid
  108. */
  109. function InitZoneId() {
  110. global $zoneid;
  111. if (isset($_REQUEST['zoneid'])) {
  112. $zoneid = $_REQUEST['zoneid']; # 从参数提取到全局变量.
  113. }
  114. }
  115. /**
  116. * 直接返回错误resp并结束运行
  117. * @param int $err
  118. * @param string $msg
  119. */
  120. function Err($err, $msg = "") {
  121. Index::Err($err, $msg);
  122. }
  123. /**
  124. * 断言快速访问
  125. * @param bool $condition
  126. * @param int/str $err
  127. */
  128. function my_Assert($condition, $err) {
  129. DebugHelper::assert($condition, $err);
  130. }
  131. /**
  132. * 带换行符输出
  133. */
  134. function echoLine() {
  135. $cli = (php_sapi_name() === 'cli');
  136. $args = func_get_args();
  137. echo implode(" ", $args) . ( $cli ? PHP_EOL : "<br/>"); # CLI下追加\n, html下追加<br>
  138. }
  139. if (GAME_ONLINE) { # 设置脚本执行的超时时间
  140. set_time_limit(25); # 外网不超过25秒
  141. } else {
  142. set_time_limit(10); # 内网不超过5秒
  143. }