Skip to content

Commit

Permalink
CloudDB custom editor configured. Generated C# model from JSON file.
Browse files Browse the repository at this point in the history
Changed all Error logs to give a better explanation about the error and error code.
Added IsSandboxActivated to HMSIAPManager.
Added ObtainOwnedPurchaseRecord method to HMSIAPManager.
Added IsRewardedAdLoaded property to HMSAdsKitManager.
Fixed SubscribeSnapshot method on CloudDB.
Fixed the bug that corrupts manager scripts on the scene when upgrading from a lower version of HMS Unity Plugin.
Fixed GetLeaderboardData method functionality.
  • Loading branch information
sametguzeldev committed Jun 18, 2021
1 parent 6a5c604 commit 4ce2682
Show file tree
Hide file tree
Showing 37 changed files with 972 additions and 301 deletions.
Binary file modified Assets/Huawei/Dlls/HuaweiMobileServices.dll
Binary file not shown.
15 changes: 15 additions & 0 deletions Assets/Huawei/Editor/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,20 @@ public static string RemoveAfter(this string value, char toRemove)
index = index == 0 ? value.Length : index;
return value.Substring(0, index);
}

public static string ToCamelCase(this string s)
{
var x = s.Replace("_", "");
if (x.Length == 0) return "null";
x = Regex.Replace(x, "([A-Z])([A-Z]+)($|[A-Z])",
m => m.Groups[1].Value + m.Groups[2].Value.ToLower() + m.Groups[3].Value);
return char.ToLower(x[0]) + x.Substring(1);
}

public static string ToPascalCase(this string s)
{
var x = ToCamelCase(s);
return char.ToUpper(x[0]) + x.Substring(1);
}
}
}
30 changes: 30 additions & 0 deletions Assets/Huawei/Editor/Utils/HMSPackageChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace HmsPlugin
{
[InitializeOnLoad]
public class HMSPackageChecker
{
static HMSPackageChecker()
{
AssetDatabase.importPackageCompleted += OnImportPackageCompleted;
}

private static void OnImportPackageCompleted(string packageName)
{
if (packageName.Contains("HMSUnityPackage"))
{
var enabledEditors = HMSMainKitsTabFactory.GetEnabledEditors();
if (enabledEditors != null && enabledEditors.Count > 0)
{
enabledEditors.ForEach(c => c.DestroyManagers());
enabledEditors.ForEach(f => f.CreateManagers());
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Huawei/Editor/Utils/HMSPackageChecker.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Huawei/Editor/View/CloudDBTab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

222 changes: 222 additions & 0 deletions Assets/Huawei/Editor/View/CloudDBTab/HMSCloudDBSettingsDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

namespace HmsPlugin
{
public class HMSCloudDBSettingsDrawer : VerticalSequenceDrawer
{
private TextField.TextField packageNameTextField;
private Label.Label jsonPathLabel;

private Root rootJson;

public HMSCloudDBSettingsDrawer()
{
packageNameTextField = new TextField.TextField("Java Package Name: ", "", OnPackageNameChanged); //TODO: reference wiki page to how to obtain java package name.
jsonPathLabel = new Label.Label();
SetupSequence();
}

private void OnPackageNameChanged(string value)
{

}

private void OnJsonFileSelected()
{
string path = EditorUtility.OpenFilePanel("Choose a json File", "", "json");
if (!string.IsNullOrEmpty(path))
{
rootJson = JsonUtility.FromJson<Root>(File.ReadAllText(path));
jsonPathLabel.SetText(Path.GetFileName(path));
}
}

private void SetupSequence()
{
AddDrawer(new HorizontalSequenceDrawer(new HorizontalLine(), new Label.Label("Creating C# Model").SetBold(true), new HorizontalLine()));
AddDrawer(new Space(3));
AddDrawer(packageNameTextField);
AddDrawer(new Space(3));
AddDrawer(new HorizontalSequenceDrawer(new Label.Label("Select Json File"), new Space(3), new Button.Button("Select", OnJsonFileSelected).SetWidth(100), jsonPathLabel));
AddDrawer(new HorizontalSequenceDrawer(new Spacer(), new Button.Button("Generate C# Models", OnGenerateButton).SetWidth(250), new Spacer()));
AddDrawer(new HorizontalLine());
AddDrawer(new Space(5));
}

private void OnGenerateButton()
{
if (rootJson != null && !string.IsNullOrEmpty(packageNameTextField.GetCurrentText()))
{
foreach (var item in rootJson.objectTypes)
{
using (var file = File.CreateText(Application.dataPath + "/Huawei/Scripts/CloudDB/" + item.objectTypeName + ".cs"))
{
file.WriteLine("using HuaweiMobileServices.CloudDB;");
file.WriteLine("using HuaweiMobileServices.Utils;");
file.WriteLine("using UnityEngine;\n");
file.WriteLine("using System;\n");
file.WriteLine("namespace HmsPlugin\n{");
file.WriteLine("\tpublic class " + item.objectTypeName + " : JavaObjectWrapper, ICloudDBZoneObject\n\t{");
file.WriteLine("\t\tpublic " + item.objectTypeName + "() : base(\"" + packageNameTextField.GetCurrentText() + "\") { }");
file.WriteLine("\t\tpublic " + item.objectTypeName + "(AndroidJavaObject javaObject) : base(javaObject) { }");
foreach (var field in item.fields)
{
file.WriteLine("\t\tprivate " + GetFieldType(field.fieldType) + " " + field.fieldName + ";");
}
file.WriteLine("");

foreach (var field in item.fields)
{
file.WriteLine(WriteProperty(field));
}

file.WriteLine("\t\tpublic AndroidJavaObject GetObj() => base.JavaObject;");
file.WriteLine("\t\tpublic void SetObj(AndroidJavaObject arg0) => base.JavaObject = arg0;");
file.WriteLine("\t}\n}");
}
}

AssetDatabase.Refresh();
}
}

private string WriteProperty(Field field)
{
StringBuilder builder = new StringBuilder();
builder.AppendLine("\t\tpublic " + GetFieldType(field.fieldType) + " " + field.fieldName.ToPascalCase() + "\n\t\t{");
switch (field.fieldType)
{
case "String":
builder.AppendLine("\t\t\tget { return Call<string>(\"get" + field.fieldName.ToPascalCase() + "\"); }");
builder.AppendLine("\t\t\tset { Call(\"set" + field.fieldName.ToPascalCase() + "\", value); }");
break;
case "Boolean":
builder.AppendLine("\t\t\tget { return Call<bool>(\"get" + field.fieldName.ToPascalCase() + "\"); }");
builder.AppendLine("\t\t\tset { Call(\"set" + field.fieldName.ToPascalCase() + "\", value); }");
break;
case "Short":
builder.AppendLine("\t\t\tget { return Call<AndroidJavaObject>(\"get" + field.fieldName.ToPascalCase() + "\").Call<short>(\"shortValue\"); }");
builder.AppendLine("\t\t\tset { Call(\"set" + field.fieldName.ToPascalCase() + "\", new AndroidJavaObject(\"java.lang.Short\", value)); }");
break;
case "Byte":
builder.AppendLine("\t\t\tget { return Call<AndroidJavaObject>(\"get" + field.fieldName.ToPascalCase() + "\").Call<byte>(\"byteValue\"); }");
builder.AppendLine("\t\t\tset { Call(\"set" + field.fieldName.ToPascalCase() + "\", new AndroidJavaObject(\"java.lang.Byte\", value)); }");
break;
case "Integer":
builder.AppendLine("\t\t\tget { return Call<AndroidJavaObject>(\"get" + field.fieldName.ToPascalCase() + "\").Call<int>(\"intValue\"); }");
builder.AppendLine("\t\t\tset { Call(\"set" + field.fieldName.ToPascalCase() + "\", new AndroidJavaObject(\"java.lang.Integer\", value)); }");
break;
case "Long":
builder.AppendLine("\t\t\tget { return Call<AndroidJavaObject>(\"get" + field.fieldName.ToPascalCase() + "\").Call<long>(\"longValue\"); }");
builder.AppendLine("\t\t\tset { Call(\"set" + field.fieldName.ToPascalCase() + "\", new AndroidJavaObject(\"java.lang.Long\", value)); }");
break;
case "Float":
builder.AppendLine("\t\t\tget { return Call<AndroidJavaObject>(\"get" + field.fieldName.ToPascalCase() + "\").Call<float>(\"floatValue\"); }");
builder.AppendLine("\t\t\tset { Call(\"set" + field.fieldName.ToPascalCase() + "\", new AndroidJavaObject(\"java.lang.Float\", value)); }");
break;
case "Double":
builder.AppendLine("\t\t\tget { return Call<AndroidJavaObject>(\"get" + field.fieldName.ToPascalCase() + "\").Call<double>(\"doubleValue\"); }");
builder.AppendLine("\t\t\tset { Call(\"set" + field.fieldName.ToPascalCase() + "\", new AndroidJavaObject(\"java.lang.Double\", value)); }");
break;
case "ByteArray":
builder.AppendLine("\t\t\tget { return Call<byte[]>(\"get" + field.fieldName.ToPascalCase() + "\"); }");
builder.AppendLine("\t\t\tset { Call(\"set" + field.fieldName.ToPascalCase() + "\", value); }");
break;
case "Text":
builder.AppendLine("\t\t\tget { return Call<AndroidJavaObject>(\"get" + field.fieldName.ToPascalCase() + "\").Call<string>(\"get\"); }");
builder.AppendLine("\t\t\tset { Call(\"set" + field.fieldName.ToPascalCase() + "\", new AndroidJavaObject(\"com.huawei.agconnect.cloud.database.Text\", value)); }");
break;
case "Date":
builder.AppendLine("\t\t\tget { return new DateTime(Call<AndroidJavaObject>(\"get" + field.fieldName.ToPascalCase() + "\").Call<long>(\"getTime\")); }");
builder.AppendLine("\t\t\tset { Call(\"set" + field.fieldName.ToPascalCase() + "\", new AndroidJavaObject(\"java.util.Date\", value.Ticks)); }");
break;
default:
break;
}
builder.AppendLine("\t\t}");
return builder.ToString();
}

private string GetFieldType(string fieldType)
{
switch (fieldType)
{
case "String":
return "string";
case "Boolean":
return "bool";
case "Short":
return "short";
case "Byte":
return "byte";
case "Integer":
return "int";
case "Long":
return "long";
case "Float":
return "float";
case "Double":
return "double";
case "ByteArray":
return "byte[]";
case "Text":
return "string";
case "Date":
return "DateTime";
default:
break;
}
return "";
}

[System.Serializable]
private class Permission
{
public string role;
public List<string> rights;
}
[System.Serializable]
private class Index
{
public string indexName;
public List<string> indexList;
}
[System.Serializable]
private class Field
{
public bool isNeedEncrypt;
public string fieldName;
public bool notNull;
public bool belongPrimaryKey;
public string fieldType;
public string defaultValue;
}
[System.Serializable]
private class ObjectType
{
public List<Index> indexes;
public string objectTypeName;
public List<Field> fields;
}
[System.Serializable]
private class Root
{
public int schemaVersion;
public List<PermissionRoot> permissions;
public List<ObjectType> objectTypes;
}
[System.Serializable]
private class PermissionRoot
{
public List<Permission> permissions;
public string objectTypeName;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Assets/Huawei/Editor/View/CloudDBTab/HMSCloudDBTabFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HmsPlugin
{
public class HMSCloudDBTabFactory
{
public static TabView CreateTab(string title)
{
var tabView = new TabView(title);
tabView.AddDrawer(new HMSCloudDBSettingsDrawer());

return tabView;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Huawei/Editor/View/CloudDBTab/HMSCloudDBTabFactory.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4ce2682

Please sign in to comment.