Advertisement
TheCountryGamer

ForgeGradle Mod Repackager

Feb 13th, 2014
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. on open itemOpened
  2.     process(itemOpened)
  3. end open
  4.  
  5. process(choose file)
  6.  
  7. to process(aFile)
  8.     set core to {"countrygamer_core", "countrygamer_countrygamer_core"}
  9.     set wean to {"weepingangels", "countrygamer_weepingangelsmod"}
  10.     set tele to {"tele", "countrygamer_tele"}
  11.     set pvz to {"pvz", "countrygamer_pvz"}
  12.    
  13.     --display dialog filePath
  14.     set colonPath to aFile as text
  15.    
  16.     set pathNames to split(colonPath, ":")
  17.    
  18.     set fullFileName to end of pathNames
  19.     set nameArray to split(fullFileName, ".jar")
  20.     set fileName to the beginning of nameArray
  21.     set fileExt to end of nameArray
  22.    
  23.     set colonPath to "Macintosh HD:"
  24.     set finderPath to "/"
  25.     set lib_colon to "Macintosh HD:"
  26.    
  27.     set pathToScriptBuild to "/"
  28.     set ignoreFolders to {"build", "libs"}
  29.    
  30.     set pathNames to pathNames's (items 2 thru -2)
  31.    
  32.     repeat with str in pathNames
  33.         set colonPath to (colonPath & str & ":")
  34.         set finderPath to (finderPath & str & "/")
  35.         if ignoreFolders does not contain str then
  36.             set pathToScriptBuild to (pathToScriptBuild & str & "/")
  37.         end if
  38.     end repeat
  39.    
  40.     -- Read build file, if exists
  41.     set mcversion to "0.0.0"
  42.     set core_version to "0.0.0"
  43.     set tele_version to "0.0.0"
  44.     set angel_version to "0.0.0"
  45.     set pvz_version to "0.0.0"
  46.     set typeList to {}
  47.     try
  48.         set file_txt to readFile(pathToScriptBuild & "custom_build.txt")
  49.         set txtLines to split(file_txt, linefeed as text)
  50.         repeat with txt in txtLines
  51.             set type_and_version to split(txt, "=")
  52.             set type to the beginning of type_and_version
  53.             set tVersion to the end of type_and_version
  54.             if type is equal to "mc" then
  55.                 set mcversion to tVersion
  56.             end if
  57.             if type is equal to "core" then
  58.                 set core_version to tVersion
  59.             end if
  60.             if type is equal to "tele" then
  61.                 set tele_version to tVersion
  62.             end if
  63.             if type is equal to "angel" then
  64.                 set angel_version to tVersion
  65.             end if
  66.             if type is equal to "pvz" then
  67.                 set pvz_version to tVersion
  68.             end if
  69.            
  70.             if type is not equal to "mc" then
  71.                 copy type to the end of typeList
  72.             end if
  73.         end repeat
  74.     end try
  75.    
  76.    
  77.     set ignoreStr to end of pathNames
  78.     repeat with str in pathNames
  79.         if str is not equal to ignoreStr then
  80.             set lib_colon to lib_colon & str & ":"
  81.         end if
  82.     end repeat
  83.    
  84.     set main_folder_colon to colonPath & fileName & ":"
  85.     set main_folder_finder to finderPath & fileName & "/"
  86.    
  87.     set java_src_colon to main_folder_colon & "com:countrygamer:"
  88.     set java_src_finder to main_folder_finder & "com/countrygamer/"
  89.     set assets_colon to main_folder_colon & "assets:"
  90.     set assets_finder to main_folder_finder & "assets/"
  91.    
  92.    
  93.     try
  94.         tell application "Finder" to delete folder (main_folder_colon)
  95.     end try
  96.     tell application "Archive Utility" to open aFile
  97.    
  98.     set java_folder_list to {"com", "countrygamer"}
  99.    
  100.     tell application "Finder"
  101.         try
  102.             delete folder (lib_colon & "Core:")
  103.             delete folder (lib_colon & "WeepingAngel:")
  104.             delete folder (lib_colon & "Tele:")
  105.             delete folder (lib_colon & "PvZ:")
  106.         end try
  107.         -- Create folder "modid" at lib_colon folder
  108.         try
  109.             make new folder at (lib_colon) with properties {name:"Core"}
  110.             make new folder at (lib_colon) with properties {name:"WeepingAngel"}
  111.             make new folder at (lib_colon) with properties {name:"Tele"}
  112.             make new folder at (lib_colon) with properties {name:"PvZ"}
  113.         end try
  114.     end tell
  115.     -- Create src folders and assets folder in "modid" folder
  116.     createFolders(lib_colon & "Core:", java_folder_list)
  117.     createFolders(lib_colon & "WeepingAngel:", java_folder_list)
  118.     createFolders(lib_colon & "Tele:", java_folder_list)
  119.     createFolders(lib_colon & "PvZ:", java_folder_list)
  120.     tell application "Finder"
  121.         try
  122.             make new folder at (lib_colon & "Core:") with properties {name:"assets"}
  123.             make new folder at (lib_colon & "WeepingAngel:") with properties {name:"assets"}
  124.             make new folder at (lib_colon & "Tele:") with properties {name:"assets"}
  125.             make new folder at (lib_colon & "PvZ:") with properties {name:"assets"}
  126.         end try
  127.     end tell
  128.     -- Repeat with the folders in the main directory
  129.     ---- Copy folders to new destinations depending on folder name
  130.     -- SRC
  131.     tell application "Finder"
  132.         set items_in_folder to folders of folder ((java_src_colon as alias))
  133.     end tell
  134.     repeat with item_in_folder in items_in_folder
  135.         --display dialog (item_in_folder as text)
  136.         set folderPath to item_in_folder as text
  137.         set folderPathNames to split(folderPath, ":")
  138.         set folderPathNames to folderPathNames's (items 1 thru -2)
  139.         set folderName to the end of folderPathNames
  140.         set typeFolderName to ""
  141.         if (core) contains folderName then
  142.             set typeFolderName to "Core"
  143.         else if wean contains folderName then
  144.             set typeFolderName to "WeepingAngel"
  145.         else if tele contains folderName then
  146.             set typeFolderName to "Tele"
  147.         else if pvz contains folderName then
  148.             set typeFolderName to "PvZ"
  149.         end if
  150.         duplicate item_in_folder to ((lib_colon & typeFolderName & ":com:countrygamer:") as alias)
  151.     end repeat
  152.     -- ASSETS
  153.     tell application "Finder"
  154.         set items_in_folder to folders of folder ((assets_colon as alias))
  155.     end tell
  156.     repeat with item_in_folder in items_in_folder
  157.         --display dialog (item_in_folder as text)
  158.         set folderPath to item_in_folder as text
  159.         set folderPathNames to split(folderPath, ":")
  160.         set folderPathNames to folderPathNames's (items 1 thru -2)
  161.         set folderName to the end of folderPathNames
  162.         set typeFolderName to ""
  163.         if core contains folderName then
  164.             set typeFolderName to "Core"
  165.         else if wean contains folderName then
  166.             set typeFolderName to "WeepingAngel"
  167.         else if tele contains folderName then
  168.             set typeFolderName to "Tele"
  169.         else if pvz contains folderName then
  170.             set typeFolderName to "PvZ"
  171.         end if
  172.         duplicate item_in_folder to ((lib_colon & typeFolderName & ":assets:") as alias)
  173.     end repeat
  174.    
  175.     -- Zip the contents of each "modid" folder and put them in the lib_colon folder
  176.     set core_zip_name to "CountryGamerCore_" & mcversion & "_" & core_version
  177.     set angel_zip_name to "CountryGamerWeepingAngels_" & mcversion & "_" & angel_version
  178.     set tele_zip_name to "CountryGamerTele_" & mcversion & "_" & tele_version
  179.     set pvz_zip_name to "CountryGamerPvZ_" & mcversion & "_" & pvz_version
  180.     tell application "Finder"
  181.         try
  182.             if typeList contains "core" then delete file (lib_colon & core_zip_name & ".zip")
  183.             if typeList contains "angel" then delete file (lib_colon & angel_zip_name & ".zip")
  184.             if typeList contains "tele" then delete file (lib_colon & tele_zip_name & ".zip")
  185.             if typeList contains "pvz" then delete file (lib_colon & pvz_zip_name & ".zip")
  186.         end try
  187.     end tell
  188.     if typeList contains "core" then
  189.         set folderAlias to (lib_colon & "Core:") as alias
  190.         zipper(folderAlias, core_zip_name)
  191.     end if
  192.     if typeList contains "angel" then
  193.         set folderAlias to (lib_colon & "WeepingAngel:") as alias
  194.         zipper(folderAlias, angel_zip_name)
  195.     end if
  196.     if typeList contains "tele" then
  197.         set folderAlias to (lib_colon & "Tele:") as alias
  198.         zipper(folderAlias, tele_zip_name)
  199.     end if
  200.     if typeList contains "pvz" then
  201.         set folderAlias to (lib_colon & "PvZ:") as alias
  202.         zipper(folderAlias, pvz_zip_name)
  203.     end if
  204.    
  205.    
  206.     -- Clean up folders
  207.     tell application "Finder"
  208.         try
  209.             delete folder (lib_colon & fileName & ":")
  210.             delete folder (lib_colon & "Core:")
  211.             delete folder (lib_colon & "WeepingAngel:")
  212.             delete folder (lib_colon & "Tele:")
  213.             delete folder (lib_colon & "PvZ:")
  214.         end try
  215.     end tell
  216.     tell application "Finder" to empty trash
  217.    
  218. end process
  219.  
  220. to split(someText, delimiter)
  221.     set AppleScript's text item delimiters to delimiter
  222.     set someText to someText's text items
  223.     set AppleScript's text item delimiters to {""} --> restore delimiters to default value
  224.     return someText
  225. end split
  226.  
  227. on readFile(unixPath)
  228.     set foo to (open for access (POSIX file unixPath))
  229.     set txt to (read foo for (get eof foo) as «class utf8»)
  230.     close access foo
  231.     return txt
  232. end readFile
  233.  
  234. to createFolders(theLocation, folderList)
  235.     set loc to theLocation
  236.     repeat with theFolder in folderList
  237.         try
  238.             --display dialog loc
  239.             tell application "Finder" to make new folder at loc with properties {name:theFolder}
  240.             set x to loc & theFolder & ":"
  241.             set loc to x
  242.         end try
  243.     end repeat
  244. end createFolders
  245.  
  246. to zipper(theItemAsAlias, newFileName)
  247.     tell application "Finder"
  248.         set itemPath to quoted form of POSIX path of theItemAsAlias
  249.         --set fileName to name of theItemAsAlias
  250.         set theFolder to POSIX path of (container of theItemAsAlias as alias)
  251.         set zipFile to quoted form of (theFolder & newFileName & ".zip")
  252.         do shell script "cd " & itemPath & ";zip -r " & zipFile & " *"
  253.     end tell
  254. end zipper
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement