player2_dz

p2_dumpFile

May 5th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQF 6.95 KB | None | 0 0
  1. /*---------------------------------------------------------------------------
  2.     Function:       p2_dumpFile
  3.     Author:         [email protected]
  4.     Description:    Takes file path as input (relative to mission root)
  5.     and outputs all of file contents regardless of file size
  6.  
  7.     Use Example:
  8.         "init.sqf" call p2_dumpFile;
  9. ---------------------------------------------------------------------------*/
  10. p2_dumpFile = {
  11.     //if you touch my script you can touch my privates too i guess
  12.     private ["_p2_dumpFile_dbg", "_p2_dumpFile_splitSize", "_p2_dumpFile_log", "_inputScriptPath", "_scriptPathArr", "_scriptPathArrLen", "_endingChar", "_startingChar", "_splitTextPart", "_splitTextArr", "_splitCount", "_waitCounter"];
  13.  
  14.     //Config
  15.     _p2_dumpFile_dbg        = 2;    //Debugging Setting: 0 = Nothing, 1 = Just Errors, 2 = Everything in logs
  16.     _p2_dumpFile_cancelMode = 1;    //0 = Off, 1 = If the function is called again, previous running versions of the function are ended without output
  17.    
  18.     //Expert config
  19.     _p2_dumpFile_splitSize  = 496; //Size to split by (dont change this m8)
  20.    
  21.     //Create log func
  22.     _p2_dumpFile_log = {
  23.         private["_in","_msg","_msgType"];
  24.         _in      = _this;
  25.         _msgType = _in select 0; //MsgTypes: 0 = Error, 1 = Information
  26.         _msg     = _in select 1;
  27.  
  28.         switch (_msgType) do {
  29.             case 0 : {  
  30.                 if (_p2_dumpFile_dbg < 1) exitWith {0};
  31.                 diag_log(format["p2_dumpFile: %1",_msg]); 0
  32.             };
  33.             case 1 : {  
  34.                 if (_p2_dumpFile_dbg > 1) exitWith {    diag_log(format["p2_dumpFile: %1",_msg]); 0 };
  35.             };
  36.             default {
  37.                 diag_log(format["p2_dumpFile: %1",_msg]); 0
  38.             };
  39.         };
  40.  
  41.         0
  42.     };
  43.    
  44.     //Log info
  45.     [1,format["Debugging Mode:                              %1", _p2_dumpFile_dbg]] call _p2_dumpFile_log;
  46.     [1,format["Cancel Mode:                                     %1", _p2_dumpFile_cancelMode]] call _p2_dumpFile_log;
  47.  
  48.     //Set Default Vars (default is mission file init)
  49.     _inputScriptPath = "init.sqf";
  50.     _scriptPathArr = toArray(_inputScriptPath);
  51.     _scriptPathArrLen = count(_scriptPathArr);
  52.     _endingChar    = 0;
  53.     _startingChar  = 0;
  54.     _splitTextPart = "";
  55.     _splitTextArr  = [];
  56.     _splitCount    = 0;
  57.     _waitCounter   = 0;
  58.  
  59.     //Check cancel mode
  60.     if (_p2_dumpFile_cancelMode > 0) then {
  61.  
  62.         //Create vars
  63.         if (isNil 'p2_dumpFile_running') then {
  64.             p2_dumpFile_running         = false;
  65.             p2_dumpFile_exitInstance    = false;
  66.         };
  67.  
  68.         //If there is an instance of this func running, set global var to true so it exits
  69.         if (p2_dumpFile_running) then {
  70.             p2_dumpFile_exitInstance    = true;
  71.             [1,format["Existing Instance Running...Waiting for it to Quit..."]] call _p2_dumpFile_log;
  72.         };
  73.  
  74.         //now wait for running to go to false, so we know the other instance quit and we can continue
  75.         //this command is why we need to use spawn not call, since we cannot halt the thread in a call
  76.         while {((p2_dumpFile_running) && (_waitCounter < 10))} do {
  77.             _waitCounter = _waitCounter + 1;
  78.             uiSleep 1;
  79.         };
  80.  
  81.         [1,format["Existing Instance Finished Running...Lets get Started!"]] call _p2_dumpFile_log;
  82.  
  83.         //Set runing var to true
  84.         p2_dumpFile_running = true;
  85.     };
  86.  
  87.     //Get input path, set to init.sqf if error or blank string
  88.     _inputScriptPath = _this;
  89.     if (isNil '_inputScriptPath' || {typeName _inputScriptPath != "STRING" || {_inputScriptPath == ""}}) exitWith {
  90.         //log the error
  91.         [0,"Input Error - Quitting"] call _p2_dumpFile_log;
  92.         //update the var
  93.         p2_dumpFile_running = false;
  94.     };
  95.  
  96.     //Get script contents and convert to array to avoid string limitations
  97.     _scriptPathArr = toArray(preprocessfilelinenumbers _inputScriptPath);
  98.  
  99.     //Get array length (character count)
  100.     _scriptPathArrLen = (count _scriptPathArr);
  101.  
  102.     //log the info
  103.     [1,format["Input Script Path:                           %1", _inputScriptPath]] call _p2_dumpFile_log;
  104.     [1,format["Input Script File Length:                    %1", _scriptPathArrLen]] call _p2_dumpFile_log;
  105.     [1,format["Split Size:                                  %1", _p2_dumpFile_splitSize]] call _p2_dumpFile_log;
  106.  
  107.     //Check if this even needs splitting
  108.     if (_scriptPathArrLen >= _p2_dumpFile_splitSize) then {
  109.  
  110.         //Get split count
  111.         _splitCount = round (_scriptPathArrLen / _p2_dumpFile_splitSize);
  112.  
  113.         //log the info
  114.         [1,format["Split Count:                                     %1", _splitCount]] call _p2_dumpFile_log;
  115.  
  116.         //For each split
  117.         for "_i" from 1 to _splitCount do {
  118.        
  119.             //Check if exit instance has been set, in which case, we should quit now!
  120.             if (p2_dumpFile_exitInstance) exitWith {
  121.                 //Log the info
  122.                 [1,format["Quitting...Another Instance has Started and Cancel Mode is 1!"]] call _p2_dumpFile_log;
  123.                 //set vars
  124.                 p2_dumpFile_exitInstance = false;
  125.                 p2_dumpFile_running = false;
  126.             };
  127.  
  128.             //Multiply amount of splits done so far by _p2_dumpFile_splitSize to get which char to end with
  129.             _endingChar = _i * _p2_dumpFile_splitSize;
  130.  
  131.             //Starting char is ending char - _p2_dumpFile_splitSize
  132.             _startingChar = _endingChar - _p2_dumpFile_splitSize;
  133.  
  134.             //Log the info
  135.             [1,format["Splitting from   %1   to   %2, chars left:   %3, splits left: %4", _startingChar,_endingChar,_scriptPathArrLen - _startingChar,_splitCount - _i]] call _p2_dumpFile_log;
  136.  
  137.             //Get chars between starting and ending and also convert to string
  138.             _splitTextPart = [_scriptPathArr,_startingChar,_endingChar] call {
  139.                 private["_inArr","_in","_pos","_len","_arr","_out"];
  140.                 _in = _this;
  141.                 _inArr= _in select 0;
  142.                 _pos=abs(_in select 1);
  143.                 _arr=[];
  144.                 for "_i" from 0 to (count _inArr)-1 do {
  145.                     _arr=_arr+[toString([_inArr select _i])];
  146.                 };
  147.                 _len=count(_arr);
  148.                 if ((count _in)>2) then {_len=(_in select 2)};
  149.                 _out="";
  150.                 if ((_pos+_len)>=(count _arr)) then {_len=(count _arr)-_pos};
  151.                 if (_len>0) then {
  152.                     for "_i" from _pos to (_pos+_len-1) do {
  153.                         _out=_out + (_arr select _i);
  154.                     };
  155.                 };
  156.                 _out  
  157.             };
  158.    
  159.             //Add part to array
  160.             _splitTextArr set [count _splitTextArr, _splitTextPart];
  161.         };
  162.  
  163.         //Check if exit instance has been set, in which case, we should quit now!
  164.         if (p2_dumpFile_exitInstance) exitWith {
  165.             //Log the info
  166.             [1,format["Quitting...Another Instance has Started and Cancel Mode is 1!"]] call _p2_dumpFile_log;
  167.             //set vars
  168.             p2_dumpFile_exitInstance = false;
  169.             p2_dumpFile_running = false;
  170.         };
  171.  
  172.         //Output
  173.         {
  174.  
  175.             //Check if exit instance has been set, in which case, we should quit now!
  176.             if (p2_dumpFile_exitInstance) exitWith {
  177.                 //Log the info
  178.                 [1,format["Quitting...Another Instance has Started and Cancel Mode is 1!"]] call _p2_dumpFile_log;
  179.                 //set vars
  180.                 p2_dumpFile_exitInstance = false;
  181.                 p2_dumpFile_running = false;
  182.             };
  183.  
  184.             diag_log(_x);
  185.         } forEach _splitTextArr;
  186.  
  187.     } else {
  188.         //Check if exit instance has been set, in which case, we should quit now!
  189.         if (p2_dumpFile_exitInstance) exitWith {
  190.             //Log the info
  191.             [1,format["Quitting...Another Instance has Started and Cancel Mode is 1!"]] call _p2_dumpFile_log;
  192.             //set vars
  193.             p2_dumpFile_exitInstance = false;
  194.             p2_dumpFile_running = false;
  195.         };
  196.  
  197.         //Output
  198.         diag_log(toString(_scriptPathArr));
  199.     };
  200.  
  201.     p2_dumpFile_running = false;
  202.     0
  203. };
Add Comment
Please, Sign In to add comment