Skip to content

Commit

Permalink
从KEngine同步
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoqingqing committed Aug 5, 2020
1 parent 016528e commit 22d3d58
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 69 deletions.
17 changes: 8 additions & 9 deletions KSFramework/Assets/Code/XLuaCustomExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,15 @@ public static List<Type> AddNameSpaceClass<T>(string namespaceName)
typeof(UnityAction<int, GameObject>),
typeof(UnityAction<int, Transform>),
typeof(AssetBundleLoader.CAssetBundleLoaderDelegate),
typeof(Action<bool>),
typeof(Action),
typeof(Func<double, double, double>),
typeof(Action<string>),
typeof(Action<string,int>),
typeof(System.Action<bool>),
typeof(System.Action<Boolean>),
typeof(Action<int>),
typeof(Action<float>),
typeof(Action<double>),
typeof(Action<bool, GameObject>),
typeof(UnityEngine.Events.UnityEvent),
typeof(UnityEngine.Events.UnityEvent<bool>),
Expand All @@ -121,13 +127,6 @@ public static List<Type> AddNameSpaceClass<T>(string namespaceName)
//typeof(TweenCallback),
typeof(UnityEngine.Events.UnityAction<UnityEngine.EventSystems.BaseEventData>),
typeof(UnityEngine.Events.UnityAction<string>),

typeof(Action),
typeof(Func<double, double, double>),
typeof(Action<string>),
typeof(Action<string,int>),
typeof(Action<int>),
typeof(Action<double>),
typeof(Action<Sprite>),
typeof(AbstractResourceLoader.LoaderDelgate),
typeof(KEngine.AssetFileLoader.AssetFileBridgeDelegate),
Expand All @@ -140,7 +139,6 @@ public static List<Type> AddNameSpaceClass<T>(string namespaceName)
typeof(KEngine.SpriteLoader.CSpriteLoaderDelegate),
typeof(System.Action<KEngine.UI.UIController, object[]>),
typeof(UnityAction<string,string>),

};

[ReflectionUse]
Expand Down Expand Up @@ -186,6 +184,7 @@ public static List<Type> AddNameSpaceClass<T>(string namespaceName)
#else
typeof(UnityEngine.Profiler),
#endif
typeof(System.Action<bool>),
};

[LuaCallCSharp]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public AppVersion(string versionStr)

if (versionArr.Length >= 5)
{
var strVerType = versionArr[5];
var strVerType = versionArr[4];
VersionType = strVerType;
}

if (versionArr.Length >= 7)
if (versionArr.Length >= 6)
{
VersionDesc = string.Join(".", versionArr, 6, versionArr.Length - 6); // 剩余的串起来
VersionDesc = string.Join(".", versionArr, 5, versionArr.Length - 5); // 剩余的串起来
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ public UIController GetOpenedWindow(string name)
if (uiBase != null && uiBase.gameObject.activeSelf) { return uiBase; }
return null;
}

public bool IsLoad(string name)
{
if (UIWindows.ContainsKey(name))
Expand Down
17 changes: 13 additions & 4 deletions KSFramework/Assets/KSFramework/KEngine/KEngine/AppEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class AppEngine : MonoBehaviour
/// <summary>
/// To Display FPS in the Debug Mode (Debug.isDebugBuild is true)
/// </summary>
static FpsWatcher RenderWatcher { get; set; } // 帧数监听器
public static FpsWatcher RenderWatcher { get; set; } // 帧数监听器

/// <summary>
/// In Init func has a check if the user has the write privillige
Expand Down Expand Up @@ -104,6 +104,12 @@ public class AppEngine : MonoBehaviour
/// </summary>
public bool IsInited { get; private set; }

public bool IsBeforeInit { get; private set; }

public bool IsOnInit { get; private set; }

public bool IsStartGame { get; private set; }

/// <summary>
/// AppEngine must be new by static function New(xxx)!
/// This is a flag to identity whether AddComponent from Unity
Expand Down Expand Up @@ -183,14 +189,17 @@ private void Init()
private IEnumerator DoInit()
{
yield return null;

IsBeforeInit = true;
if (AppEntry != null)
{
yield return StartCoroutine(AppEntry.OnBeforeInit());
}


// if (GameModules != null)
IsOnInit = true;
yield return StartCoroutine(DoInitModules(GameModules));

IsStartGame = true;
if (AppEntry != null)
{
yield return StartCoroutine(AppEntry.OnGameStart());
Expand Down Expand Up @@ -312,7 +321,7 @@ public enum KEngineDefaultConfigs
SettingExt,
}

class FpsWatcher
public class FpsWatcher
{
private float Value;
private float Sensitivity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ protected virtual void Init(string url, params object[] args)

protected virtual void OnFinish(object resultObj)
{
Action doFinish = () =>
{
// Action doFinish = () =>
// {
// 如果ReadyDispose,无效!不用传入最终结果!
ResultObject = resultObj;

Expand All @@ -369,9 +369,9 @@ protected virtual void OnFinish(object resultObj)
//Dispose();
Log.Trace("[BaseResourceLoader:OnFinish]时,准备Disposed {0}", Url);
}
};

doFinish();
// };
//
// doFinish();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ public static AssetBundleLoader Load(string url, CAssetBundleLoaderDelegate call
private static AssetBundleManifest _assetBundleManifest;
/// <summary>
/// Unity5下,使用manifest进行AssetBundle的加载
/// bool isForce,在热更新后,可能需要强制刷新AssetBundleManifest。
/// </summary>
static void PreLoadManifest()
public static void PreLoadManifest(bool isForce = false)
{
if (_hasPreloadAssetBundleManifest)
if (_hasPreloadAssetBundleManifest && isForce == false)
return;

_hasPreloadAssetBundleManifest = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ public Object Asset
}

private bool IsLoadAssetBundle;
public static bool IsEditorLoadAsset
{
get
{
return AppEngine.GetConfig("KEngine", "IsEditorLoadAsset").ToInt32() != 0 && Application.isEditor;
}
}

public override float Progress
{
Expand All @@ -69,7 +62,7 @@ public override float Progress
public static AssetFileLoader Load(string path, AssetFileBridgeDelegate assetFileLoadedCallback = null, LoaderMode loaderMode = LoaderMode.Async)
{
// 添加扩展名
if (!IsEditorLoadAsset)
if (!KResourceModule.IsEditorLoadAsset)
path = path + AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt);

LoaderDelgate realcallback = null;
Expand All @@ -81,6 +74,22 @@ public static AssetFileLoader Load(string path, AssetFileBridgeDelegate assetFil
return AutoNew<AssetFileLoader>(path, realcallback, false, loaderMode);
}

/// <summary>
/// Check Bundles/[Platform]/xxxx.kk exists?
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static bool IsBundleResourceExist(string url)
{
if (KResourceModule.IsEditorLoadAsset)
{
var editorPath = "Assets/" + KEngineDef.ResourcesBuildDir + "/" + url;
var hasEditorUrl = File.Exists(editorPath);
if (hasEditorUrl) return true;
}

return KResourceModule.IsResourceExist(KResourceModule.BundlesPathRelative + url.ToLower() + AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt));
}
protected override void Init(string url, params object[] args)
{
var loaderMode = (LoaderMode)args[0];
Expand All @@ -95,7 +104,7 @@ private IEnumerator _Init(string path, LoaderMode loaderMode)

Object getAsset = null;

if (IsEditorLoadAsset)
if (KResourceModule.IsEditorLoadAsset)
{
#if UNITY_EDITOR
if (path.EndsWith(".unity"))
Expand All @@ -116,8 +125,6 @@ private IEnumerator _Init(string path, LoaderMode loaderMode)
Log.Error("`IsEditorLoadAsset` is Unity Editor only");

#endif
OnFinish(getAsset);

}
else if (!IsLoadAssetBundle)
{
Expand All @@ -129,7 +136,6 @@ private IEnumerator _Init(string path, LoaderMode loaderMode)
{
Log.Error("Asset is NULL(from Resources Folder): {0}", path);
}
OnFinish(getAsset);
}
else
{
Expand Down Expand Up @@ -227,7 +233,7 @@ private IEnumerator _Init(string path, LoaderMode loaderMode)
}
}

if (getAsset != null && IsLoadAssetBundle)
if (getAsset != null)
{
// 更名~ 注明来源asset bundle 带有类型
getAsset.name = String.Format("{0}~{1}", getAsset, Url);
Expand Down Expand Up @@ -255,10 +261,10 @@ public static void RefreshMaterialsShaders(Renderer renderer)
protected override void DoDispose()
{
base.DoDispose();

if (_bundleLoader != null)
{
_bundleLoader.Release(); // 释放Bundle(WebStream)
}

//if (IsFinished)
{
if (!IsLoadAssetBundle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,59 @@ public static HotBytesLoader Load(string path, LoaderMode loaderMode)

private string _fullUrl;

private IEnumerator CoLoad(string url)
/// <summary>
/// Convenient method to load file sync auto.
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static byte[] LoadSync(string url)
{
var getResPathType = KResourceModule.GetResourceFullPath(url, _loaderMode == LoaderMode.Async, out _fullUrl);
string fullUrl;
var getResPathType = KResourceModule.GetResourceFullPath(url, false, out fullUrl);
if (getResPathType == KResourceModule.GetResourceFullPathType.Invalid)
{
if (Debug.isDebugBuild)
Log.Error("[HotBytesLoader]Error Path: {0}", url);
OnFinish(null);
yield break;
return null;
}

if (_loaderMode == LoaderMode.Sync)
byte[] bytes;
if (getResPathType == KResourceModule.GetResourceFullPathType.InApp)
{
// 存在应用内的,StreamingAssets内的,同步读取;否则去PersitentDataPath
if (getResPathType == KResourceModule.GetResourceFullPathType.InApp)
if (Application.isEditor) // Editor mode : 读取Product配置目录
{
if (Application.isEditor) // Editor mode : 读取Product配置目录
{
var loadSyncPath = Path.Combine(KResourceModule.ProductPathWithoutFileProtocol, url);
Bytes = KResourceModule.ReadAllBytes(loadSyncPath);
}
else // product mode: read streamingAssetsPath
{
Bytes = KResourceModule.LoadSyncFromStreamingAssets(url);
}
var loadSyncPath = Path.Combine(KResourceModule.ProductPathWithoutFileProtocol, url);
bytes = KResourceModule.ReadAllBytes(loadSyncPath);
}
else
else // product mode: read streamingAssetsPath
{
Bytes = KResourceModule.ReadAllBytes(_fullUrl);
bytes = KResourceModule.LoadSyncFromStreamingAssets(url);
}
}
else
{
bytes = KResourceModule.ReadAllBytes(fullUrl);
}
return bytes;
}

private IEnumerator CoLoad(string url)
{
if (_loaderMode == LoaderMode.Sync)
{
Bytes = LoadSync(url);
}
else
{

var getResPathType = KResourceModule.GetResourceFullPath(url, _loaderMode == LoaderMode.Async, out _fullUrl);
if (getResPathType == KResourceModule.GetResourceFullPathType.Invalid)
{
if (Debug.isDebugBuild)
Log.Error("[HotBytesLoader]Error Path: {0}", url);
OnFinish(null);
yield break;
}

_wwwLoader = KWWWLoader.Load(_fullUrl);
while (!_wwwLoader.IsCompleted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,20 @@ public static bool IsAssetExists(string path)
public static string GetAssetString(string path)
{
#if !KENGINE_DLL && UNITY_ANDROID
#if UNITY_2017_1_OR_NEWER
var clsPtr = AndroidJNI.FindClass("com/github/KEngine/AndroidHelper");
var methondPtr_getAssetBytes = AndroidJNIHelper.GetMethodID(clsPtr, "getAssetBytes", "(Ljava/lang/String;)[B", true);
var arg1 = new object[] { path };
var args = AndroidJNIHelper.CreateJNIArgArray(arg1);
var byteArray = AndroidJNI.CallStaticObjectMethod(clsPtr, methondPtr_getAssetBytes, args);
var bytes = AndroidJNI.FromSByteArray(byteArray);
AndroidJNIHelper.DeleteJNIArgArray(arg1, args);
AndroidJNI.DeleteLocalRef(byteArray);
AndroidJNI.DeleteLocalRef(clsPtr);
return bytes.ToString();
#else
return AndroidHelper.CallStatic<string>("getAssetString", path);
#endif
#else
ErrorNotSupport();
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ public enum GetResourceFullPathType
InDocument,
}

public static bool IsResourceExist(string url)
{
string fullPath;
var hasDocUrl = TryGetDocumentResourceUrl(url, false, out fullPath);

var hasInAppUrl = TryGetInAppStreamingUrl(url, false, out fullPath);
return hasDocUrl || hasInAppUrl;
}

/// <summary>
/// 根据相对路径,获取到StreamingAssets完整路径,或Resources中的路径
/// </summary>
Expand Down Expand Up @@ -273,6 +282,14 @@ public static string GetAppDataPath()
return Application.persistentDataPath;
}

public static bool IsEditorLoadAsset
{
get
{
return AppEngine.GetConfig("KEngine", "IsEditorLoadAsset").ToInt32() != 0 && Application.isEditor;
}
}


/// <summary>
/// (not android ) only! Android资源不在目录!
Expand Down Expand Up @@ -330,6 +347,7 @@ private static bool FileExistsWithDifferentCase(string filePath)
string[] files = Directory.GetFiles(directory, fileTitle);
var realFilePath = files[0].Replace("\\", "/");
filePath = filePath.Replace("\\", "/");
filePath = filePath.Replace("//", "/");

return String.CompareOrdinal(realFilePath, filePath) == 0;
}
Expand Down
Loading

0 comments on commit 22d3d58

Please sign in to comment.