Guest User

Kalphiter's original autosaver for Blockland

a guest
Jan 3rd, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. $kaphost::AS::dir = "base/server/kaphost/autosave";
  2.  
  3. if(isFile("Add-Ons/System_ReturnToBlockland/server.cs"))
  4. {
  5. if(!$RTB::RTBR_ServerControl_Hook)
  6. exec("Add-Ons/System_ReturnToBlockland/RTBR_ServerControl_Hook.cs");
  7.  
  8. RTB_registerPref("Interval", "Autosaver", "$kaphost::AS::interval", "int 0 180", "Server_AutoSaver", 5, 0, 0);
  9. RTB_registerPref("Announce", "Autosaver", "$kaphost::AS::announce", "bool", "Server_AutoSaver", 1, 0, 0);
  10. }
  11. else
  12. {
  13. $kaphost::AS::announce = false;
  14. $kaphost::AS::interval = 5;
  15. }
  16.  
  17. function kaphost_AS1_begin()
  18. {
  19. deleteVariables("$kaphost::AS_groups::group*");
  20.  
  21. cancel($kaphost_AS_schedule);
  22.  
  23. if($kaphost::AS::announce)
  24. messageAll('', "\c5Autosaving...");
  25.  
  26. $time_beg = getSimTime();
  27.  
  28. %dir = $kaphost::AS::dir;
  29. $kaphost::AS_groups::group_count = 0;
  30. $kaphost::AS_groups::cur_group = 0;
  31. $kaphost::AS_groups::brick_count = 0;
  32.  
  33. for(%i = 0; %i < mainBrickGroup.getCount(); %i++)
  34. {
  35. %g = mainBrickGroup.getObject(%i);
  36. %b = %g.getCount();
  37. if(%b > 0)
  38. {
  39. %g.kaphost_AS_stop = %b;
  40. $kaphost::AS_groups::group[$kaphost::AS_groups::group_count] = %g;
  41. $kaphost::AS_groups::group_count++;
  42. }
  43. }
  44.  
  45. $kaphost::AS::FO = %f = new FileObject();
  46. %f.openForWrite(%dir @"/TEMP1.bls");
  47.  
  48. if(isObject($kaphost::AS_groups::group[0]))
  49. kaphost_AS1_nextGroup(%f);
  50. }
  51.  
  52. function kaphost_AS1_nextGroup(%f)
  53. {
  54. if($kaphost::AS_groups::cur_group == $kaphost::AS_groups::group_count)
  55. return kaphost_AS1_end(%f);
  56.  
  57. %g = $kaphost::AS_groups::group[$kaphost::AS_groups::cur_group];
  58. $kaphost::AS_groups::cur_group++;
  59. kaphost_AS1_nextBrick(%f, %g, 0);
  60. }
  61.  
  62. function kaphost_AS1_nextBrick(%f, %g, %c)
  63. {
  64. if(%c >= %g.getCount())
  65. return kaphost_AS1_nextGroup(%f);
  66.  
  67. %f.writeLine(%g.getObject(%c).getID());
  68.  
  69. $kaphost::AS_groups::brick_count += 1;
  70.  
  71. schedule(0, 0, kaphost_AS1_nextBrick, %f, %g, %c+1);
  72. }
  73.  
  74. function kaphost_AS1_end(%f)
  75. {
  76. %f.close();
  77. %f.delete();
  78.  
  79. //Phase 1 complete
  80. kaphost_AS2_begin();
  81. }
  82.  
  83. function kaphost_AS2_begin()
  84. {
  85. %events = 1;
  86. %ownership = 1;
  87.  
  88. %dir = $kaphost::AS::dir;
  89.  
  90. %f_A = new FileObject();
  91. %f_B = new FileObject();
  92. %f_A.path = %dir @"/TEMP1.bls";
  93. %f_B.path = %dir @"/TEMP2.bls";
  94.  
  95. %f_B.openForWrite(%f_B.path);
  96. %f_B.writeLine("This is a Blockland save file. You probably shouldn't modify it cause you'll screw it up.");
  97. %f_B.writeLine("1");
  98. %f_B.writeLine(%desc);
  99.  
  100. for(%i = 0; %i < 64; %i++)
  101. %f_B.writeLine(getColorIDTable(%i));
  102.  
  103. %f_B.writeLine("Linecount "@ $kaphost::AS_groups::brick_count);
  104.  
  105. $kaphost::AS_groups::brick_count = 0;
  106.  
  107. %f_A.openForRead(%f_A.path);
  108. kaphost_AS2_nextLine(%f_A, %f_B);
  109. }
  110.  
  111. function kaphost_AS2_nextLine(%f_A, %f_B, %c)
  112. {
  113. %c++;
  114.  
  115. if(!%f_A.isEOF())
  116. {
  117. %brick = %f_A.readLine();
  118. if(isObject(%brick))
  119. {
  120. $kaphost::AS_groups::brick_count++;
  121. if(%brick.getDataBlock().hasPrint)
  122. {
  123. %texture = getPrintTexture(%brick.getPrintId());
  124. %path = filePath(%texture);
  125. %underscorePos = strPos(%path, "_");
  126. %name = getSubStr(%path, %underscorePos + 1, strPos(%path, "_", 14) - 14) @ "/" @ fileBase(%texture);
  127. if($printNameTable[%name] !$= "")
  128. %print = %name;
  129. }
  130.  
  131. %f_B.writeLine(%brick.getDataBlock().uiName @ "\" " @ %brick.getPosition() SPC %brick.getAngleID() SPC %brick.isBasePlate() SPC %brick.getColorID() SPC %print SPC %brick.getColorFXID() SPC %brick.getShapeFXID() SPC %brick.isRayCasting() SPC %brick.isColliding() SPC %brick.isRendering());
  132.  
  133. if((%ownership = 1) && !$Server::LAN)
  134. %f_B.writeLine("+-OWNER " @ getBrickGroupFromObject(%brick).bl_id);
  135.  
  136. if(%events = 1)
  137. {
  138. if(%brick.getName() !$= "")
  139. %f_B.writeLine("+-NTOBJECTNAME " @ %brick.getName());
  140.  
  141. for(%b = 0; %b < %brick.numEvents; %b++)
  142. {
  143. %targetClass = %brick.eventTargetIdx[%b] >= 0 ? getWord(getField($InputEvent_TargetListfxDTSBrick_[%brick.eventInputIdx[%b]], %brick.eventTargetIdx[%b]), 1) : "fxDtsBrick";
  144. %paramList = $OutputEvent_parameterList[%targetClass, %brick.eventOutputIdx[%b]];
  145. %params = "";
  146. for(%c = 0; %c < 4; %c++)
  147. {
  148. if(firstWord(getField(%paramList, %c)) $= "dataBlock" && isObject(%brick.eventOutputParameter[%b, %c + 1]))
  149. %params = %params TAB %brick.eventOutputParameter[%b, %c + 1];
  150. else
  151. %params = %params TAB %brick.eventOutputParameter[%b, %c + 1];
  152. }
  153. %f_B.writeLine("+-EVENT" TAB %b TAB %brick.eventEnabled[%b] TAB %brick.eventInput[%b] TAB %brick.eventDelay[%b] TAB %brick.eventTarget[%b] TAB %brick.eventNT[%b] TAB %brick.eventOutput[%b] @ %params);
  154. }
  155. }
  156. if(isObject(%brick.emitter))
  157. %f_B.writeLine("+-EMITTER " @ %brick.emitter.emitter.uiName @ "\" " @ %brick.emitterDirection);
  158.  
  159. if(%brick.getLightID() >= 0)
  160. %f_B.writeLine("+-LIGHT " @ %brick.getLightID().getDataBlock().uiName @ "\" "); // Not sure if something else comes after the name
  161.  
  162. if(isObject(%brick.item))
  163. %f_B.writeLine("+-ITEM " @ %brick.item.getDataBlock().uiName @ "\" " @ %brick.itemPosition SPC %brick.itemDirection SPC %brick.itemRespawnTime);
  164.  
  165. if(isObject(%brick.audioEmitter))
  166. %f_B.writeLine("+-AUDIOEMITTER " @ %brick.audioEmitter.getProfileID().uiName @ "\" "); // Not sure if something else comes after the name
  167.  
  168. if(isObject(%brick.vehicleSpawnMarker))
  169. %f_B.writeLine("+-VEHICLE " @ %brick.vehicleSpawnMarker.uiName @ "\" " @ %brick.reColorVehicle);
  170. }
  171.  
  172. return schedule(0, 0, kaphost_AS2_nextLine, %f_A, %f_B, %c);
  173. }
  174.  
  175. else
  176. {
  177. kaphost_AS2_end(%f_A, %f_B);
  178. }
  179. }
  180.  
  181. function kaphost_AS2_end(%f_A, %f_B)
  182. {
  183. %f_A.close();
  184. %f_B.close();
  185. %dir = $kaphost::AS::dir;
  186.  
  187. for(%a = 5; %a >= 0; %a--)
  188. if(isFile(%dir @"/auto"@ %a @".bls"))
  189. {
  190. if(%a == 5)
  191. fileDelete(%dir @"/auto"@ %a @".bls");
  192.  
  193. else
  194. fileCopy(%dir @"/auto"@ %a @".bls", %dir @"/auto"@ (%a+1) @".bls");
  195. }
  196.  
  197. fileCopy(%f_B.path, %dir @"/auto0.bls");
  198. fileDelete(%f_A.path);
  199. fileDelete(%f_B.path);
  200.  
  201. %f_A.delete();
  202. %f_B.delete();
  203.  
  204. if($kaphost::AS::interval < 5)
  205. $kaphost::AS::interval = 1;
  206.  
  207. if($kaphost::AS::interval > 180)
  208. $kaphost::AS::interval = 180;
  209.  
  210. %diff = (getSimTime() - $time_beg);
  211.  
  212. if($kaphost::AS::announce)
  213. messageAll('', "\c5Autosaved \c6"@ $kaphost::AS_groups::brick_count @" \c5bricks in \c6"@ (%diff/1000) @" \c5seconds.");
  214.  
  215. %next_time = 60 * 1000 - %diff;
  216. %next_time = $kaphost::AS::interval * 60 * 1000 - %diff;
  217. if(%next_time < 60000)
  218. %next_time = $kaphost::AS::interval * 60 * 1000;
  219.  
  220. $kaphost_AS_schedule = schedule(%next_time, 0, kaphost_AS1_begin);
  221. }
  222.  
  223. $kaphost_AS_schedule = schedule(60 * 7.5 * 1000, 0, kaphost_AS1_begin);
Advertisement
Add Comment
Please, Sign In to add comment