Skip to content

Commit

Permalink
Merge pull request #2 from VRLabs/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jellejurre authored Feb 26, 2024
2 parents 7c5d0b5 + f15e60a commit e9590b4
Show file tree
Hide file tree
Showing 11 changed files with 401 additions and 1 deletion.
84 changes: 84 additions & 0 deletions .github/workflows/VRC-Asset-Release-And-Upload.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: VRC Asset Release and Listing Upload
on:
push:
tags:
- "*.*.*"

env:
ASSETS_PATH: .
RELEASE_PATH: Packages
ARTIFACT_DURATION: 30 # In days
UPLOAD_ENDPOINT: https://api.vrlabs.dev/packages/add
WORKFLOW_VERSION: 1.0.0

jobs:
build:
runs-on: "ubuntu-latest"
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Check if package.json exists
run: |
if [ ! -f package.json ]; then
echo "package.json not found"
exit 1
fi
- name: Get package.json
id: get_package_json
run: |
{
echo 'package_json<<"""'
echo $(cat package.json)
echo '"""'
} >> $GITHUB_OUTPUT
- name: Get needed Data
id: job_data
run: |
version=$(echo "${{ github.ref_name }}")
version=$(echo $version | tr '[:upper:]' '[:lower:]')
echo "version=$version" >> $GITHUB_OUTPUT
major_version=$(echo $version | cut -d '.' -f 1)
minor_version=$(echo $version | cut -d '.' -f 2)
echo "major_version=$major_version" >> $GITHUB_OUTPUT
echo "minor_version=$minor_version" >> $GITHUB_OUTPUT
name="${{ fromJson(steps.get_package_json.outputs.package_json).name }}"
display_name="${{ fromJson(steps.get_package_json.outputs.package_json).displayName }}"
echo "package_name=$name" >> $GITHUB_OUTPUT
echo "package_display_name=$display_name" >> $GITHUB_OUTPUT
- name: Create Packages
id: create_packages
uses: VRLabs/VRCTools-Packaging-Action@v1
with:
path: '${{ env.ASSETS_PATH }}'
outputPath: '${{ env.RELEASE_PATH }}'
releaseUrl: 'https://github.com/${{ github.repository }}/releases/download/${{ steps.job_data.outputs.version }}/${{ steps.job_data.outputs.package_name }}-${{ steps.job_data.outputs.version }}.zip'
unityReleaseUrl: 'https://github.com/${{ github.repository }}/releases/download/${{ steps.job_data.outputs.version }}/${{ steps.job_data.outputs.package_name }}-${{ steps.job_data.outputs.version }}.unitypackage'
releaseVersion: '${{ steps.job_data.outputs.version }}'

- name: Create Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
name: "${{ steps.job_data.outputs.package_display_name }} ${{ steps.job_data.outputs.version }}"
files: |
${{ steps.create_packages.outputs.unityPackagePath }}
${{ steps.create_packages.outputs.vccPackagePath }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SOURCE_TAG: ${{ steps.job_data.outputs.version }}

- name: Add server-json to Artifacts
uses: actions/upload-artifact@v3
with:
name: server-json
path: ${{ steps.create_packages.outputs.serverPackageJsonPath }}
retention-days: ${{ env.ARTIFACT_DURATION }}

- name: Send package info to a server
run: |
curl -X POST -H "Content-Type: application/json" -H "Vrl-Api-Key: ${{ secrets.LISTINGS_API_KEY }}" --data @${{ steps.create_packages.outputs.serverPackageJsonPath }} ${{ env.UPLOAD_ENDPOINT }} || exit 0
shell: bash
16 changes: 16 additions & 0 deletions Instancer.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "VRLabs Instancer",
"rootNamespace": "",
"references": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Instancer.asmdef.meta

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

185 changes: 185 additions & 0 deletions Instancer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace VRLabs.Instancer
{
public class Instancer : MonoBehaviour
{
public static string Instance(string packageName, string installFilePath, string[] excludeRegexs)
{
string targetFolder = EditorUtility.OpenFolderPanel("Select Directory To Copy Assets To", "Assets/", "");

if (targetFolder == "" || targetFolder == null)
{
Debug.LogError("No folder selected, please select a folder to copy the assets to.");
return null;
}

if (!targetFolder.Contains(Application.dataPath))
{
Debug.LogError("Selected folder is not in the Assets folder, please select a folder in the Assets directory.");
return null;
}

targetFolder = PrepareTargetFolderPath(targetFolder, packageName);

string sourceFolder = GetSourceFolder(installFilePath);

string[] localAssetPaths = GetLocalAssetPaths(sourceFolder, excludeRegexs);

CreateDirectories(localAssetPaths, targetFolder);

CopyFiles(localAssetPaths, sourceFolder, targetFolder);

FixReferences(localAssetPaths, sourceFolder, targetFolder);

AssetDatabase.Refresh();

return targetFolder;
}

public static void Install(string packageName, string installFilePath, string[] excludeRegexs, Action<string> callBack)
{
string instancePath = Instance(packageName, installFilePath, excludeRegexs);
callBack(instancePath);
}

static string PrepareTargetFolderPath(string folderPath, string packageName)
{
folderPath = "Assets" + folderPath.Remove(0, Application.dataPath.Length) + "/" + packageName;

if (Directory.Exists(folderPath))
{
int i = 1;
while (Directory.Exists(folderPath + i.ToString()))
{
i++;
}

folderPath += i;
}

Directory.CreateDirectory(folderPath);
AssetDatabase.ImportAsset(folderPath);
return folderPath;
}

static string GetSourceFolder(string installFilePath)
{
string sourceFolder = installFilePath;

while (!File.Exists(sourceFolder + "/package.json"))
{
sourceFolder = Path.GetDirectoryName(sourceFolder);
}

return sourceFolder.Replace("\\", "/");
}

static string[] GetLocalAssetPaths(string sourceFolder, string[] excludeRegexs)
{
string[] assetPaths = AssetDatabase.FindAssets("", new [] { sourceFolder }).Select(AssetDatabase.GUIDToAssetPath).ToArray();

string[] filteredLocalAssetPaths = assetPaths
.Select(path => path.Remove(0,sourceFolder.Length))
.Where(path => excludeRegexs.All(regex => !Regex.Match(path, regex).Success))
.ToArray();

return filteredLocalAssetPaths;
}

static void CreateDirectories(string[] filePaths, string targetFolder)
{
try
{
AssetDatabase.StartAssetEditing();
foreach (string path in filePaths)
{
string targetPath = Path.GetDirectoryName(targetFolder + path);
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
AssetDatabase.ImportAsset(targetPath);
}
}
}
finally
{
AssetDatabase.StopAssetEditing();
}
}

static void CopyFiles(string[] filePaths, string sourceFolder, string targetFolder)
{
try
{
AssetDatabase.StartAssetEditing();
foreach (string path in filePaths)
{
if (!Directory.Exists(sourceFolder + path))
{
AssetDatabase.CopyAsset(sourceFolder + path, targetFolder + path);
}
}
}
finally{
AssetDatabase.StopAssetEditing();
}
}

static void FixReferences(string[] localAssetPaths, string sourceFolder, string targetFolder)
{
foreach (string localAssetPath in localAssetPaths)
{
string targetAssetPath = targetFolder + localAssetPath;
UnityEngine.Object[] targetAssets = AssetDatabase.LoadAllAssetsAtPath(targetAssetPath);
foreach (var targetAsset in targetAssets)
{
SerializedObject serializedObject = new SerializedObject(targetAsset);
SerializedProperty property = serializedObject.GetIterator();
do
{
if (property.propertyType == SerializedPropertyType.ObjectReference)
{
// Debug.Log($"O{property.name}, {property.displayName}, {property.objectReferenceValue}");
if (property.objectReferenceValue != null)
{
property.objectReferenceValue = GetTargetVersion(sourceFolder, targetFolder, property.objectReferenceValue);
}
}

if (property.propertyType == SerializedPropertyType.ExposedReference)
{
// Debug.Log($"E{property.name}, {property.displayName}, {property.exposedReferenceValue}");
if (property.exposedReferenceValue != null)
{
property.exposedReferenceValue = GetTargetVersion(sourceFolder, targetFolder, property.exposedReferenceValue);
}
}
} while (property.Next(true));

serializedObject.ApplyModifiedProperties();
}
}
}

private static Object GetTargetVersion(string sourceFolder, string targetFolder, Object target)
{
string targetPath = AssetDatabase.GetAssetPath(target);
if (targetPath.StartsWith(sourceFolder))
{
string newTargetPath = targetFolder + targetPath.Remove(0, sourceFolder.Length);
return AssetDatabase.LoadAllAssetsAtPath(newTargetPath).Where(obj => obj.GetType() == target.GetType()).FirstOrDefault(x => x.name == target.name);
}

return target;
}
}
}
11 changes: 11 additions & 0 deletions Instancer.cs.meta

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

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 VRLabs
Copyright (c) 2022 VRLabs LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 7 additions & 0 deletions LICENSE.meta

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

54 changes: 54 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<div align="center">

# Instancer

[![Generic badge](https://img.shields.io/github/downloads/VRLabs/Instancer/total?label=Downloads)](https://github.com/VRLabs/Instancer/releases/latest)
[![Generic badge](https://img.shields.io/badge/License-MIT-informational.svg)](https://github.com/VRLabs/Instancer/blob/main/LICENSE)
[![Generic badge](https://img.shields.io/badge/Unity-2019.4.31f1-lightblue.svg)](https://unity3d.com/unity/whats-new/2019.4.31)
[![Generic badge](https://img.shields.io/badge/SDK-AvatarSDK3-lightblue.svg)](https://vrchat.com/home/download)

[![Generic badge](https://img.shields.io/discord/706913824607043605?color=%237289da&label=DISCORD&logo=Discord&style=for-the-badge)](https://discord.vrlabs.dev/)
[![Generic badge](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.vercel.app%2Fapi%3Fusername%3Dvrlabs%26type%3Dpatrons&style=for-the-badge)](https://patreon.vrlabs.dev/)

VRLabs' Instancing system that copies files over for use in the Assets directory and can also runs install scripts

### ⬇️ [Download Latest Version](https://github.com/VRLabs/Instancer/releases/latest)


---

## How it works

* It gets called by a small dummy file included in the package, and when called copies all the files over, and replaces references to old files with references to new files.
* It can also run an install script callback once it's done with this.

## Install guide

* It should generally only be installed as a dependency of a VRLabs package

## How to use

* Click the `VRLabs/[PackageName]` button in the toolbar and select an output folder to copy to.

## Contributors

* [Jelle](https://jellejurre.dev)
*
## License

Instancer is available as-is under MIT. For more information see [LICENSE](https://github.com/VRLabs/Instancer/blob/main/LICENSE).


<div align="center">

[<img src="https://github.com/VRLabs/Resources/raw/main/Icons/VRLabs.png" width="50" height="50">](https://vrlabs.dev "VRLabs")
<img src="https://github.com/VRLabs/Resources/raw/main/Icons/Empty.png" width="10">
[<img src="https://github.com/VRLabs/Resources/raw/main/Icons/Discord.png" width="50" height="50">](https://discord.vrlabs.dev/ "VRLabs")
<img src="https://github.com/VRLabs/Resources/raw/main/Icons/Empty.png" width="10">
[<img src="https://github.com/VRLabs/Resources/raw/main/Icons/Patreon.png" width="50" height="50">](https://patreon.vrlabs.dev/ "VRLabs")
<img src="https://github.com/VRLabs/Resources/raw/main/Icons/Empty.png" width="10">
[<img src="https://github.com/VRLabs/Resources/raw/main/Icons/Twitter.png" width="50" height="50">](https://twitter.com/vrlabsdev "VRLabs")

</div>

7 changes: 7 additions & 0 deletions Readme.md.meta

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

Loading

0 comments on commit e9590b4

Please sign in to comment.