SamsonGame/gamectl

386 lines
9.8 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
ini_set('memory_limit', -1);
gc_disable();
gamectl_check_lock();
$GLOBALS['GAME_ROOT'] = dirname(__FILE__).'/';
if(file_exists(__DIR__.'/utils/composer/vendor/composer/autoload_real.php'))
include_once(__DIR__.'/utils/composer/vendor/autoload.php');
include_once(__DIR__.'/utils/taskman.inc.php');
$GLOBALS["TASKMAN_HELP_FUNC"] = function() { taskman_default_usage('gamectl'); };
function _error_handler($errno, $errstr, $errfile, $errline)
{
if($errno == E_STRICT)
return;
// @ sign temporary disabled error reporting
if(error_reporting() == 0)
return;
$err = "Error happened: $errno, $errstr, $errfile, $errline\n";
throw new Exception($err);
}
function _exception_handler($e)
{
$msg = "".$e;
$stderr = fopen('php://stderr', 'a');
fwrite($stderr, $msg);
fclose($stderr);
exit(1);
}
set_error_handler("_error_handler");
set_exception_handler("_exception_handler");
gamectl_setup_env($argv);
foreach(glob(dirname(__FILE__).'/gamectl.d/*.php') as $script)
include_once($script);
gamectl_include_env();
taskman_run($argv);
function gamectl_setup_env(&$argv)
{
global $GAME_ROOT;
$env = '';
$filtered = array();
for($i=0;$i<sizeof($argv);++$i)
{
$v = $argv[$i];
if(strpos($v, "--env") === 0)
{
list($tmp, $env) = explode("=", $v);
continue;
}
else
$filtered[] = $v;
}
$argv = $filtered;
//GAME_ENV env. variable has top priority
if(getenv("GAME_ENV"))
$env = getenv("GAME_ENV");
if($env)
putenv("GAME_ENV=$env");
//default one
else
putenv("GAME_ENV=");
}
function gamectl_include_env()
{
global $GAME_ROOT;
$env_filename = get_env_file();
if($env_filename && !file_exists($env_filename))
throw new Exception("Error setting environment. No such env file $env_filename");
else if($env_filename === null && file_exists("$GAME_ROOT/gamectl.props.php"))
$env_filename = "$GAME_ROOT/gamectl.props.php";
if($env_filename)
include($env_filename);
}
function get_env_file()
{
global $GAME_ROOT;
$env = '';
if(getenv("GAME_ENV"))
$env = getenv("GAME_ENV");
if($env)
$env_filename = $GAME_ROOT . "/maint/env/$env.props.php";
else
$env_filename = $GAME_ROOT . '/gamectl.props.php';
if(file_exists($env_filename))
return $env_filename;
return null;
}
function get_build_deps()
{
global $GAME_ROOT;
return array(
$GAME_ROOT . '/gamectl',
get_env_file(),
$GAME_ROOT . '/VERSION',
);
}
function gen_build_file($tpl_file, $out_file)
{
gen_file(
$tpl_file,
$out_file,
get_build_deps(),
taskman_prop('FORCE_BUILD') ? 1/*force anyway*/ : 2/*write only if contents differs*/
);
}
function game_version_full_str()
{
global $GAME_ROOT;
return trim(file($GAME_ROOT . "/VERSION")[0]);
}
function game_version()
{
$full_str = game_version_full_str();
$has_patch_code = substr_count($full_str, '.') > 2;
if(!$has_patch_code)
return $full_str;
return substr($full_str, 0, strrpos($full_str, '.'));
}
function game_version_code($str = null)
{
$str = $str === null ? game_version() : $str;
$items = explode('.', $str);
if(sizeof($items) < 2)
throw new Exception("Bad version format: $str");
$maj = $items[0];
$min = $items[1];
$pat = isset($items[2]) ? $items[2] : 0;
$num = $pat + $min*100 + $maj*1000000;
return $num;
}
function game_patch_code()
{
$full_str = game_version_full_str();
$has_patch_code = substr_count($full_str, '.') > 2;
if(!$has_patch_code)
return 0;
$code = substr($full_str, strrpos($full_str, '.')+1);
if($code > 9)
throw new Exception("Patch code must be <= 9");
return $code;
}
function make_full_package_id($separator)
{
$base_id = taskman_prop("PACKAGE_ID");
$suffix = taskman_prop("PACKAGE_ID_SUFFIX");
if(!$suffix || strlen($suffix) == 0)
return $base_id;
return "$base_id$separator$suffix";
}
/**
* @always
*/
function task_setup_props()
{
global $GAME_ROOT;
//General
taskman_propsetor("GAME_COMPANY_NAME", "Play Today");
taskman_propsetor("GAME_PROJECT_NAME", "game_prototype");
taskman_propsetor("CLIENT_APP_NAME", "Game Prototype");
taskman_propsetor("CLIENT_APP_ICON", "Assets/logo/icon.png");
//Users
taskman_propsetor("TESTFLIGHT_ACCOUNT_USERNAME", 'a.barsukov@bit.games');
taskman_propsetor("TESTFLIGHT_ACCOUNT_PASSWORD", 'ahye-wpzn-naab-bquh');
taskman_propsetor("UNITY_CONNECT_PURCHASING", 0);
taskman_propsetor("ADS_MEDIATION", "");
taskman_propsetor("ADS_MEDIATION_KEY", "");
taskman_propsetor("ADS_BANNER_UNIT", "");
taskman_propsetor("ADS_INTERSTITIAL_UNIT", "");
taskman_propsetor("ADS_REWARD_UNIT", "");
taskman_propsetor("MOPUB_ADMOD_APP_ID", "");
taskman_propsetor("GA_SETTINGS_ASSET", "");
taskman_propsetor("ADVERTY_SETTINGS_ASSET", "");
taskman_propsetor("FACEBOOK_APP_ID", "");
taskman_propsetor("TENJIN_KEY", "");
taskman_propsetor("IOS_APP_ID", "");
taskman_propsetor("IOS_TEAM_ID", "GST5PEH37B");
taskman_propsetor("PACKAGE_ID", "com.goplaytoday.gameprototype");
//Suffix allows to designate variants of the app (e.g. beta or staging)
//while still treating them as one app in analytics (because of same base PACKAGE_ID)
taskman_propsetor("PACKAGE_ID_SUFFIX", "");
//iOS only accepts dots and hyphens in bundle id
//while Android only accepts dots and underscores
taskman_propset("PACKAGE_ID_FULL_IOS", make_full_package_id("-"));
taskman_propset("PACKAGE_ID_FULL_ANDROID", make_full_package_id("_"));
taskman_propsetor("DATA_FILE", "game.dat");
taskman_propset("GAME_ROOT", $GAME_ROOT);
taskman_propset("GAME_VERSION", game_version());
taskman_propset("GAME_VERSION_CODE", game_version_code());
taskman_propset("GAME_PATCH_CODE", game_patch_code());
//allowing to be passed from above
taskman_propsetor("GAME_REVISION_HASH", function() { return substr(git_get_rev_hash(), 0, 5);});
taskman_propsetor("GAME_VERSION_BRANCH", function() { return git_get_branch(); });
taskman_propset("GAME_REVISION_NUMBER", git_get_rev_number());
taskman_propsetor("OFFLINE_REWARD", 0);
taskman_propsetor("DAILY_REWARD", 0);
taskman_propsetor("LEVEL_REWARD", 0);
taskman_propsetor("BUFF_SECTION", 0);
taskman_propsetor("SHOP_BUTTON", 0);
taskman_propsetor("PLIST_INSTALL_ADHOC_URL", "http://172.19.179.141/" . taskman_prop("GAME_PROJECT_NAME") . "_adhoc.ipa");
// taskman_propsetor("SMCS_DEFAULT_ARGS", "-warnaserror+ -nowarn:4014 -nowarn:1030");
taskman_propsetor("SMCS_DEFAULT_ARGS", "-nowarn:4014 -nowarn:1030");
taskman_propsetor("SMCS_DEFINES", "");//separated by comma
taskman_propset("CONFIGS_BUNDLE_NAME", "packed_bundle.bytes");
taskman_propsetor('GAME_IS_DEV', 1);
taskman_propsetor("FORCE_BUILD", 0);
taskman_propsetor("CONF_PACK_BHL", 0);
taskman_propsetor("USE_LZ4_CONFIGS", function_exists('lz4_compress') ? 1 : 0);
taskman_propsetor("USE_CONFIGS_REFS", 0);
taskman_propsetor("APP_PREFIX", "mrunner");
taskman_propsetor('UNITY_APP_DIR', function() { return guess_unity_app_dir(); });
taskman_propsetor("BUILD_CLIENT_OPTS", "None");
taskman_propsetor("ANDROID_TARGET_STORE", "GPLAY");
taskman_propsetor("ANDROID_USE_IL2CPP", 1);
taskman_propsetor("ANDROID_APK_DEVICE_ARCHS", 'ARMv7,ARM64'); //May be either ARMv7, ARM64, X86 or All. Comma separated, no spaces.
taskman_propsetor("ANDROID_NATIVE_DEBUG_SYMBOLS_ZIP", 0);
taskman_propsetor("ANDROID_GRADLE_PLUGIN_VERSION", "3.4.3");
//NOTE: needed for str2num validation
define('MTG_CONF_BASE_PATH', "$GAME_ROOT/configs/");
}
function task_props($args = array())
{
$filter = '';
if(isset($args[0]))
$filter = $args[0];
$props = taskman_getprops();
echo "\n";
echo "Available props:\n";
foreach($props as $k => $v)
{
if($filter && stripos($k, $filter) === false)
continue;
echo "---------------------------------\n";
echo "$k : " . var_export($v, true) . "\n";
}
echo "\n";
}
function task_version()
{
echo "Version: " . game_version() . "\n";
echo "Version code:" . game_version_code() . "\n";
}
/**
* @alias urun
* @deps autogen,client_dlls,pack_configs,setup_unity_player_settings,unity_defines
*/
function task_ultimate_build_run()
{}
function task_setup_hooks()
{
global $GAME_ROOT;
$template = "$GAME_ROOT/utils/git/post-merge";
$hook = "$GAME_ROOT.git/hooks/post-merge";
ensure_copy($template, $hook);
chmod($hook, 0755);
}
function task_clean()
{
global $GAME_ROOT;
ensure_rm("$GAME_ROOT/build/tmp");
ensure_rm("$GAME_ROOT/build/xcode_archives");
ensure_rm("$GAME_ROOT/build_xcode");
ensure_rm("$GAME_ROOT/build/ipa");
ensure_rm("$GAME_ROOT/build/ext_config");
ensure_rm("$GAME_ROOT/utils/autogen");
ensure_rm("$GAME_ROOT/dll_src/bhl/autogen.cs");
ensure_rm("$GAME_ROOT/dll_src/metagen/autogen");
ensure_rm("$GAME_ROOT/Assets/Resources/ext_config/");
ensure_rm("$GAME_ROOT/Assets/Scripts/Settings.cs");
$dlls = glob("$GAME_ROOT/Assets/Plugins/game_*.dll");
foreach($dlls as $dll)
ensure_rm($dll);
//bhl_clean();
}
function task_envs()
{
global $GAME_ROOT;
$dir = $GAME_ROOT."maint/env/";
echo "=== Available envs ===\n";
$results = scan_files_rec(array($dir), array(".props.php"));
foreach($results as $file)
echo str_replace(".props.php", "", str_replace($dir, "", $file)) . "\n";
}
function gamectl_check_lock()
{
if(getenv("GAMECTL_IN_LOCK"))
return;
//don't bother with windows
if(DIRECTORY_SEPARATOR != '/')
return;
$lock = dirname(__FILE__) . '/gamectl.lock';
while(file_exists($lock))
{
echo "gamectl is locked. Waiting...\n";
$pid = file_get_contents($lock);
if(!trim(exec("lsof -p $pid")))
{
echo "Seems gamectl process is gone but lock still exists, proceeding..\n";
break;
}
sleep(1);
}
register_shutdown_function('gamectl_unlock');
putenv("GAMECTL_IN_LOCK=1");
file_put_contents($lock, getmypid());
}
function gamectl_unlock()
{
$lock = dirname(__FILE__) . '/gamectl.lock';
if(file_exists($lock))
unlink($lock);
}