Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the missing .meta for copied library. #1153

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 82 additions & 34 deletions unity/Editor/Bindings/MujocoBinaryRetriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,95 @@
using UnityEditor.PackageManager;
using UnityEngine;

namespace Mujoco {
public class MujocoBinaryRetriever {
namespace Mujoco
{
public class MujocoBinaryRetriever
{

[InitializeOnLoadMethod]
static void SubscribeToEvent() {
// This causes the method to be invoked after the Editor registers the new list of packages.
Events.registeredPackages += RegisteredPackagesEventHandler;
}
[InitializeOnLoadMethod]
static void SubscribeToEvent()
{
// This causes the method to be invoked after the Editor registers the new list of packages.
Events.registeredPackages += RegisteredPackagesEventHandler;
}

static void RegisteredPackagesEventHandler(
PackageRegistrationEventArgs packageRegistrationEventArgs) {
foreach (var packageInfo in packageRegistrationEventArgs.added) {
if (packageInfo.name.Equals("org.mujoco")) {
var mujocoPath = packageInfo.assetPath;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
if (AssetDatabase.LoadMainAssetAtPath(mujocoPath + "/mujoco.dylib") == null) {
File.Copy(
"/Applications/MuJoCo.app/Contents/Frameworks" +
"/mujoco.framework/Versions/Current/libmujoco.3.0.1.dylib",
mujocoPath + "/mujoco.dylib");
AssetDatabase.Refresh();
static void RegisteredPackagesEventHandler(
PackageRegistrationEventArgs packageRegistrationEventArgs)
{
foreach (var packageInfo in packageRegistrationEventArgs.added)
{
if (packageInfo.name.Equals("org.mujoco"))
{
var mujocoPath = packageInfo.assetPath;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
if (AssetDatabase.LoadMainAssetAtPath(mujocoPath + "/mujoco.dylib") == null)
{
File.Copy(
"/Applications/MuJoCo.app/Contents/Frameworks" +
"/mujoco.framework/Versions/Current/libmujoco.3.0.1.dylib",
mujocoPath + "/mujoco.dylib");
GenerateMetaFile(mujocoPath + "/mujoco.dylib");
AssetDatabase.Refresh();

}
}
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
if (AssetDatabase.LoadMainAssetAtPath(mujocoPath + "/libmujoco.so") == null) {
File.Copy(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) +
"/.mujoco/mujoco-3.0.1/lib/libmujoco.so.3.0.1",
mujocoPath + "/libmujoco.so");
AssetDatabase.Refresh();
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
if (AssetDatabase.LoadMainAssetAtPath(mujocoPath + "/libmujoco.so") == null)
{
File.Copy(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) +
"/.mujoco/mujoco-3.0.1/lib/libmujoco.so.3.0.1",
mujocoPath + "/libmujoco.so");
GenerateMetaFile(mujocoPath + "/libmujoco.so");
AssetDatabase.Refresh();

}
}
} else {
if (AssetDatabase.LoadMainAssetAtPath(mujocoPath + "/mujoco.dll") == null) {
File.Copy(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) +
"\\MuJoCo\\bin\\mujoco.dll",
mujocoPath + "\\mujoco.dll");
AssetDatabase.Refresh();
else
{
if (AssetDatabase.LoadMainAssetAtPath(mujocoPath + "/mujoco.dll") == null)
{
File.Copy(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) +
"\\MuJoCo\\bin\\mujoco.dll",
mujocoPath + "\\mujoco.dll");
GenerateMetaFile(mujocoPath + "\\mujoco.dll");
AssetDatabase.Refresh();

}
}
}
}
}
static void GenerateMetaFile(string file)
{
string metaFileName = file + ".meta";

// Check if .meta file exists.
if (File.Exists(metaFileName))
{
Console.WriteLine("Meta file for " + file + " is already created!");
return;
}

// Create a uuid
var uuidN = Guid.NewGuid().ToString("N");

// Write content to .meta
using (StreamWriter writer = File.CreateText(metaFileName))
{
writer.WriteLine("fileFormatVersion: 2");
writer.WriteLine("guid: " + (uuidN.ToString()));
writer.WriteLine("DefaultImporter:");
writer.WriteLine(" externalObjects: {}");
writer.WriteLine(" userData: ");
writer.WriteLine(" assetBundleName: ");
writer.WriteLine(" assetBundleVariant: ");
}

Console.WriteLine("Meta file for " + file + "created!");
}
}
}
}