[Unity]Unity调用SVN更新

边写边学批处理,真心累!有一个致命Bug不能解决很伤心。。。等什么时候批处理大成再来改。

SVN更新代码:

@echo off
:: ******************************************************
::在user_name=后面填上自己的SVN账号
set user_name=username
::在password_str=后面填上自己的SVN密码
::注意在符号"前加上反斜杠符号\
set password_str=password
:: ******************************************************

echo *****************************************************
echo SVN自动更新
echo 有冲突,以服务器为准
echo *****************************************************
::更新路径是外部传入的,这里是Unity传过来的
set update_path=%1%

echo SVN cleanup...
svn cleanup "%update_path%"

echo 更新目录 "%update_path%"
::echo svn update "%update_path%" --username "%user_name%" --password "%password_str%" --accept theirs-full
svn update "%update_path%" --username "%user_name%" --password "%password_str%" --accept theirs-full

pause
exit

这里有个致命Bug,就是在没有证书的情况下,会弹出要求验证证书:

Error validating server certificate for ‘https://xxxxxxx‘: 
......
<R>eject, accept <t>emporarily or accept <p>ermanently?

然后手动输入p,再回车,就能执行更新。不能实现一键更新,网上搜了一天也没找到解决的办法,就先放弃了。

试过批处理echo的回答功能,没用。SVN的全局参数--non-interactive --trust-server-cert也没有用。

SVN提交代码:

@echo off
echo ******************************************************
echo SVN一键提交
echo ******************************************************

set config_path=%~dp0CommitFileConfig.txt

::检查是否有上传配置文件
if not exist "%config_path%" (
echo %~dp0路径下没有CommitFileConfig.txt文件
pause
exit
)

for /f "tokens=*" %%a in ('type "%config_path%"') do (
	if exist "%%a" (
		echo 上传文件目录 "%%a"
		::添加新文件
		svn add "%%a" --force
		::上传变更内容
		svn commit -m"SVN工具测试" "%%a"
	)
)

pause
exit

上面的是批处理代码,Unity上调用就简单了。

Unity代码:

using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using System.Diagnostics;
using System.Threading;
using System.IO;

public class SVNTools
{
    enum SVN_COMMAND_TYPE
    {
        UPDATE,
        COMMIT,
        LOG,
    }

    [MenuItem("SVN/一键更新")]
    private static void SVNToolUpdateAll()
    {
        SVNBatCommandAndRun(SVN_COMMAND_TYPE.UPDATE);
    }

    [MenuItem("SVN/提交资源")]
    private static void SVNToolCommitAll()
    {
        SVNBatCommandAndRun(SVN_COMMAND_TYPE.COMMIT);
    }

    [MenuItem("SVN/全部日志")]
    private static void SVNToolLogAll()
    {
        SVNCommandAndRun(SVN_COMMAND_TYPE.LOG);
    }

    /// <summary>
    /// 获取选中文件路径
    /// </summary>
    /// <returns></returns>
    private static string GetSelectPath()
    {
        List<string> assetPaths = new List<string>();
        string path = null;
        foreach (var guid in UnityEditor.Selection.assetGUIDs)
        {
            if (string.IsNullOrEmpty(guid)) continue;

            path = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
            if (!string.IsNullOrEmpty(path))
                assetPaths.Add(path);
        }

        if (assetPaths == null) return null;

        path = "";
        string assetPath = null;
        for (int i = 0; i < assetPaths.Count; ++i)
        {
            assetPath = assetPaths[i];
            if (i != 0)
                path += "*";
            path += assetPath;
        }

        return path;
    }

    /// <summary>
    /// 调用小乌龟SVN
    /// </summary>
    /// <param name="commandType"></param>
    /// <param name="path"></param>
    private static void SVNCommandAndRun(SVN_COMMAND_TYPE commandType, string path = null)
    {
        #region //拼接小乌龟svn命令
        string command = "TortoiseProc.exe /command:";
        switch (commandType)
        {
            case SVN_COMMAND_TYPE.UPDATE:
                command += "update /path:\"";
                break;
            case SVN_COMMAND_TYPE.COMMIT:
                command += "commit /path:\"";
                break;
            case SVN_COMMAND_TYPE.LOG:
            default:
                command += "log /path:\"";
                break;
        }
        if (path == null || path == "")
        {
            command += Application.dataPath;
            command = command.Substring(0, command.Length - 6);
        }
        else
            command += path;
        command += "\"";
        command += " /closeonend:0";
        //UnityEngine.Debug.LogError("command: " + command);
        #endregion

        // 新开线程防止锁死
        Thread newThread = new Thread(new ParameterizedThreadStart(SVNCmdThread));
        newThread.Start(command);
    }

    private static void SVNCmdThread(object obj)
    {
        Process p = new Process();
        p.StartInfo.FileName = "cmd.exe";
        //UnityEngine.Debug.LogError("command: " + obj.ToString());
        p.StartInfo.Arguments = "/c " + obj.ToString();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.Start();

        p.WaitForExit();
        p.Close();
    }


    private static void SVNBatCommandAndRun(SVN_COMMAND_TYPE commandType)
    {
        string updatePath = Application.dataPath;
        updatePath = updatePath.Substring(0, updatePath.Length - 6);

        Thread newThread = null;
        switch (commandType)
        {
            case SVN_COMMAND_TYPE.UPDATE:
                newThread = new Thread(new ParameterizedThreadStart(UpdateBatThread));
                
                break;
            case SVN_COMMAND_TYPE.COMMIT:
                newThread = new Thread(new ParameterizedThreadStart(CommitBatThread));
                break;
        }
        // 新开线程防止锁死
        if (newThread != null)
            newThread.Start(updatePath);
    }

    private static void UpdateBatThread(object obj)
    {
        string updatePath = obj.ToString();
        string batFilePath = updatePath + "/Tools/SVNTools/SVNUpdate.bat";

        Process p = new Process();
        p.StartInfo.FileName = batFilePath;
        p.StartInfo.Arguments = updatePath;
        p.Start();

        p.WaitForExit();
        p.Close();
    }

    private static void CommitBatThread(object obj)
    {
        string batFilePath = obj.ToString() + "/Tools/SVNTools/SVNCommit.bat";

        Process p = new Process();
        p.StartInfo.FileName = batFilePath;
        p.Start();

        p.WaitForExit();
        p.Close();
    }
}

代码也挺简单的,也就没注释。

为了方便,顺便也把自己的git批处理写了一遍:

@echo off

::更新路径
set direct_path=G:\_Code\_DirectX_Projects\
set unity_path=G:\_Code\_Unity_Projects\
set unreal_path=G:\_Code\_Unreal_Projects\

echo ************************************************************
echo create by zp
echo 一键更新Git仓库
echo Direct folder: "%direct_path%"
echo Unity folder: "%unity_path%"
echo Unreal folder: "%unreal_path%"
echo ************************************************************

echo 开始更新Direct Projects...
cd /d %direct_path%
git fetch
git pull

::换行符
echo\
echo 开始更新Unity Projects...
cd /d %unity_path%
git fetch
git pull

echo\
echo 开始更新Unreal Projects...
cd /d %unreal_path%
git fetch
git pull

echo ************************************************************
echo 更新完成
echo ************************************************************

pause
exit
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值