我对编程有点陌生。我没有去学校学习它,而是在网上阅读了很多关于编码的知识。
一般来说,如果我知道一个概念,那么我通常可以用谷歌弄清楚。
从帖子中可以明显看出,我喜欢玩游戏,所以我想有一天在为 mod 配置一些设置时,我意识到这些信息非常难以挖掘。我认为我可以尝试通过编写 python 脚本来解决它(在这一点之前编写脚本没有真正的成功)。
以下是我在 python 中处理它之前编写的一般工作流程:
将“Mods”之前的基本路径替换为“/ Game / Mods”
在 Mods 文件夹中查找文件的所有路径
重命名以 uet 结尾的所有 mod 文件。例如 filname.uet-& gt;filename.filename
从 Mod 子目录中的二进制文件解析 Mod 的名称,将所有相关路径的整数名称替换为文本名称。例如,mod 名称 899987403-& gt;Primal_Fear_Bosses
之前的示例(ubuntu 路径):/ mnt / c / 程序文件(x86)/ Steam / steamapps / common / ARK / ShooterGame / Content / Mods / 899987403 / 香草 / 弹丸 / Manticore / Proj / PFBProjManticoreQuill.uet
& amp;在脚本之后:/ 游戏 / Mods / Primal_Fear_Bosses / 香草 / 弹丸 / Manticore / Proj / PFBProjManticoreQuill.PFBProjManticoreQuill,
* 注意:对齐两行的“/ Mods /”部分以查看关键差异。
无论如何,我熟悉了很多的概念,在这一点上,所以我做了很多谷歌搜索,写了下面的脚本(第一次),“Generate_Resource_Pulling_List_for_S +.py”:
import os
import sys
from fnmatch import fnmatch
import re
# define the directory that mods are installed in
# ubuntu path
# modDir = "C:/Program Files (x86)/Steam/steamapps/common/ARK/ShooterGame/Content/Mods"
# windows path
modDir = "C:\Program Files (x86)\Steam\steamapps\common\ARK\ShooterGame\Content\Mods\\"
stringPattern = r"\w+(.mod)"
regexPattern = re.compile(stringPattern)
modFolders = [mod for mod in os.listdir(modDir) if not regexPattern.match(mod)]
# loop through mod list and append directory to mod installation path
for items in modFolders:
modPath = "%s%s" % (modDir, items)
# p mod name from meta file, store for later
modMetaFilePath = "%s\%s" % (modPath, "modmeta.info")
validPath = os.path.exists(modMetaFilePath)
if not validPath:
print('x is:', validPath)
p
try:
modFile = open(modMetaFilePath, "rb")
binaryContent = modFile.read(-1)
modFile.close()
# if len == 0:
# print('length:', len(binaryContent))
# break
# print(type(binaryContent))
text = binaryContent.decode('UTF-8')
try:
pdModName = text.split('Mods/')[1].split('/')[0]
if not pdModName:
break
except ValueError as e:
p
except Exception as e:
p
for path, subdirs, files in os.walk(modPath):
# for i in range(len(subdirs)):
# print('Number of Sub Directories: ', len(subdirs))
# print('Current Directory Number in ', path, ': ', subdirs[i])
for name in files:
pattern = "*.uet"
if fnmatch(name, pattern):
try:
path = os.path.normpath(path) + '\\'
if not path:
continue
try:
basePath = os.path.join('\\Game\\Mods\\', pdModName)
except Exception as e:
p
print('failed here:', str(e))
strippedBasePath = os.path.dirname(path).split(items)[1]
if not strippedBasePath:
print('failed at this point stripped path', strippedBasePath, '\n\t', 'path', path)
continue
revisedFileName = os.path.join(
os.path.basename(name).split('.')[0] + '.' + os.path.basename(name).split('.')[0] + ',')
finalPath = "%s%s\%s" % (basePath, strippedBasePath, revisedFileName)
flippedSlashFinalPath = finalPath.replace("\\", "/")
print(flippedSlashFinalPath)
with open("out.txt", "a") as external_file:
add_text = flippedSlashFinalPath
external_file.write(add_text)
external_file.close()
except Exception as e:
print('Something happened', str(e))
我最初在 Windows 上安装了一个 Ubuntu 环境,因为我不熟悉 Windows 中的命令行 / bash / 脚本,如 mod 路径所示(斜杠颠倒,命令不同)。
我认为这个脚本将由其他人使用一点编程知识,但它不是最用户友好的。
无论如何,这是我第一次尝试写东西并尝试使用良好的做法。
作为练习更多编码的另一个侧面项目,我重写了它并实现了更复杂的概念。
这里是重构版本:
import os
import sys
from fnmatch import fnmatch
import re
import winreg
stringPattern = r"\w+(.mod)"
regexPattern = re.compile(stringPattern)
cl Mod(object):
def __init__(self):
p
self.steam_path = None
self.mod_folders = []
self.mod_path = None
self.mod_path_list = []
self.mod_meta_path_list = []
self.full_mod_path = None
self.mod_meta = None
self.resource_path = None
@property
def GetSteamPath(self):
try:
steam_key = "SOFTWARE\WOW6432Node\Valve\Steam"
hkey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, steam_key)
except ValueError as e:
hkey = None
print(e.sys.exc_info())
try:
steam_path = winreg.QueryValueEx(hkey, "InstallPath")
except ValueError as e:
steam_path = None
print(e.sys.exc_info())
winreg.CloseKey(hkey)
self.steam_path = steam_path[0]
return self.steam_path
def GetArkModPath(self):
mod_dir = self.GetSteamPath[:] + "\steamapps\common\ARK\ShooterGame\Content\Mods\\"
self.mod_folders = [mod for mod in os.listdir(mod_dir) if not regexPattern.match(mod)]
mod_path_list = []
count = 1
while count < len(self.mod_folders):
for mod in self.mod_folders:
self.full_mod_path = ("%s%s\\" % (mod_dir, mod))
mod_path_list.append(self.full_mod_path)
self.mod_path_list = mod_path_list
if not mod:
break
# else:
# print('Test1', format(mod_path_list))
return self.mod_path_list
count += 1
def GetModResourceList(self, mod_path_list):
mod_folder_index = 0
for mod_path in mod_path_list:
mod_meta_path = "%s%s" % (mod_path, "modmeta.info")
self.mod_meta_path_list.append(mod_meta_path)
validPath = os.path.exists(mod_meta_path)
if not validPath:
# print('No Mod Meta File found at: ', mod_meta_path)
continue
try:
mod_file = open(mod_meta_path, "rb")
binary_content = mod_file.read(-1)
mod_file.close()
text = binary_content.decode('UTF-8')
try:
pd_mod_name = text.split('Mods/')[1].split('/')[0]
if not pd_mod_name:
break
except ValueError as e:
p
except Exception as e:
p
for path, subdirs, files in os.walk(mod_path):
# for i in range(len(subdirs)):
# print('Number of Sub Directories: ', len(subdirs))
# print('Current Directory Number in ', path, ': ', subdirs[i])
for uet_file in files:
pattern = "*.uet"
if fnmatch(uet_file, pattern):
try:
path = os.path.normpath(path) + '\\'
if not path:
continue
try:
base_path = os.path.join('\\Game\\Mods\\', pd_mod_name)
except Exception as e:
p
print('failed here:', str(e))
stripped_base_path = os.path.dirname(path).split(self.mod_folders[mod_folder_index])[1]
resource_name = os.path.join(
os.path.basename(uet_file).split('.')[0] + '.' + os.path.basename(uet_file).split('.')[0] + ',')
full_path = "%s%s\%s" % (base_path, stripped_base_path, resource_name)
resource_path = full_path.replace("\\", "/")
self.resource_path = resource_path
# to see what text is written to the file, uncomment print here
print(self.resource_path)
with open("test_out.txt", "a") as external_file:
add_text = self.resource_path
external_file.write(add_text)
external_file.close()
except Exception as e:
print('Error: ', str(e))
# return self.resource_path[]
mod_folder_index += 1
ark = Mod()
ark_mods = ark.GetArkModPath()
ark.GetModResourceList(ark_mods)
这个修订后的版本更加用户友好,不一定需要输入或修改变量。我试图避免要求在方法调用中传递参数,因为我想在没有用户任何输入的情况下根据需要自动执行脚本。
如果你有方舟安装了一些 mod,那么你可能能够实际使用或测试脚本。我很好奇他们是否为他人工作。仅供参考,并非所有路径都可用。最好删除 / 删除一些或只是挑出你想要的。无法真正找出识别这一点的最佳方法。
我努力创建我的类的实例,设置什么属性,设置什么属性,为我的类的属性设置(?)值,调用这些存储值,以及几乎所有你能想象的东西。
有些事情对我来说是显而易见的,比如在一个不一定被重用的脚本中创建类和方法是毫无意义的。它仍然很有趣,重点是学习和实践。
在任何情况下,我对任何反馈都持开放态度,因为我不知道什么是一切的常识或其他人已经看到的最佳实践。作为旁注,我使用本地 repos 进行版本控制。我以前使用过 git 和 bitbucket。
首选主题,如果不确定:防御性编程适当的异常处理 SOLID(我在这里缺乏)类(我几乎没有得到我的工作,如果它不是 apparrent)方法(似乎直截了当-如果代码要被复制,把它放在一个方法)首选的命名法的一切,类,方法,变量(我用谷歌搜索了它,但仍然不喜欢它的样子,特别是当它们变长时)
欢迎链接到资源!我非常感谢任何反馈,并期待所有评论。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(65条)