Skip to content

Instantly share code, notes, and snippets.

@ChainSwordCS
Created December 11, 2024 23:48
Show Gist options
  • Save ChainSwordCS/cf9e56918fdb7da7821664acd55e5076 to your computer and use it in GitHub Desktop.
Save ChainSwordCS/cf9e56918fdb7da7821664acd55e5076 to your computer and use it in GitHub Desktop.
a quick and dirty script designed to recursively extract files from Unity `.asset` YAML files, which were extracted using AssetRipper
'''
UNLICENSE
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org/>
'''
# a quick and dirty script designed to recursively extract files from Unity `.asset` YAML files, which were extracted using AssetRipper
# last updated 2024-12-11
# python 3.12.7+
import os
def process_text_file(file_path):
with open(file_path, 'r') as file:
f = file.read()
# find "Name" field, automatically fall back to "m_Name" field if not found.
p = f.find("m_Name: ")
if (p == -1):
p = -8 # search will start at position 0
p = f.find("Name: ", p+8)
if (p == -1):
print("failed to extract "+file_path)
return
p2 = f.find("\n", p+6) # might cause text encoding issues, not sure
if (p2 == -1):
print("failed to extract "+file_path)
return
extractedfilename = f[p+6:p2]
p3 = f.find("Bytes: ")
if (p3 == -1):
print("failed to extract "+file_path)
return
p3 = p3 + 7
end = f.find("\n", p3)
if (end == -1):
end = len(f)
with open(os.path.join(os.getcwd(),"out",extractedfilename), "wb") as extractedfile:
extractedfile.write(bytes.fromhex(f[p3:end]))
return
# recursive
def traverse_directory(directory_path):
for entry in os.scandir(directory_path):
if entry.is_file() and entry.name.endswith('.asset'):
process_text_file(entry.path)
elif entry.is_dir():
traverse_directory(entry.path)
# main():
workdir = os.getcwd()
outdir = os.path.join(workdir, "out")
if not os.path.exists(outdir):
os.makedirs(outdir)
traverse_directory(workdir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment