Compare commits

..

10 Commits

Author SHA1 Message Date
Lucien Greathouse
300975f49c 0.4.13 2018-11-12 21:51:48 -08:00
Lucien Greathouse
325cf56457 Update CHANGES and test-project 2018-11-12 21:47:08 -08:00
Lucien Greathouse
71b1320aa8 Issue a warning instead of dying when a partition target does not exist 2018-11-12 21:45:28 -08:00
Lucien Greathouse
c5abc87dbd Increase polling interval to 300ms 2018-08-17 11:24:05 -07:00
Lucien Greathouse
bd7ab593d5 Release 0.4.12 2018-06-21 11:21:42 -07:00
Lucien Greathouse
c93da3f6b2 Update CHANGES 2018-06-21 11:19:22 -07:00
Lucien Greathouse
8b90e98696 Added a plugin action for the sync in command (#80) 2018-06-21 11:17:26 -07:00
Lucien Greathouse
bc40ec8a5a Update CHANGES 2018-06-21 11:11:03 -07:00
Lucien Greathouse
f19cbccdd5 Fix assertion failure when renaming files.
Fixes #78.
2018-06-21 11:10:28 -07:00
Lucien Greathouse
f25ae914e4 Add TODO about preserving partition roots in reconciliation 2018-06-20 18:23:24 -07:00
128 changed files with 3707 additions and 7343 deletions

View File

@@ -3,16 +3,13 @@ root = true
[*]
end_of_line = lf
charset = utf-8
insert_final_newline = false
trim_trailing_whitespace = true
[*.{json,js,css}]
[*.json]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
[*.{md,rs}]
[*.md]
indent_style = space
indent_size = 4
[*.lua]
indent_style = tab
trim_trailing_whitespace = true

4
.gitignore vendored
View File

@@ -1,5 +1 @@
/site
/target
/server/scratch
**/*.rs.bk
/generate-docs.run

View File

@@ -29,10 +29,11 @@ matrix:
rust: stable
script:
- cd server
- cargo test --verbose
- language: rust
rust: beta
script:
- cargo test --verbose
- cd server
- cargo test --verbose

View File

@@ -1,29 +1,7 @@
# Rojo Change Log
## Current master
* "Epiphany" rewrite, in progress since the beginning of time
* New live sync protocol
* Uses HTTP long polling to reduce request count and improve responsiveness
* New project format
* Hierarchical, preventing overlapping partitions
* Added `rojo build` command
* Generates `rbxm`, `rbxmx`, `rbxl`, or `rbxlx` files out of your project
* Usage: `rojo build <PROJECT> --output <OUTPUT>.rbxm`
* Added `rojo upload` command
* Generates and uploads a place or model to roblox.com out of your project
* Usage: `rojo upload <PROJECT> --cookie "<ROBLOSECURITY>" --asset_id <PLACE_ID>`
* New plugin
* Only one button now, "Connect"
* New UI to pick server address and port
* Better error reporting
* Added support for `.csv` files turning into `LocalizationTable` instances
* Added support for `.txt` files turning into `StringValue` instances
* Added debug visualization code to diagnose problems
* `/visualize/rbx` and `/visualize/imfs` show instance and file state respectively; they require GraphViz to be installed on your machine.
* Added optional place ID restrictions to project files
* This helps prevent syncing in content to the wrong place
* Multiple places can be specified, like when building a multi-place game
* Added support for specifying properties on services in project files
* *No changes*
## 0.4.13 (November 12, 2018)
* When `rojo.json` points to a file or directory that does not exist, Rojo now issues a warning instead of throwing an error and exiting

2068
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +0,0 @@
[workspace]
members = [
"server",
"rojo-e2e",
]

49
DESIGN.md Normal file
View File

@@ -0,0 +1,49 @@
# Rojo Design - Protocol Version 1
This is a super rough draft that I'm trying to use to lay out some of my thoughts.
## API
### POST `/read`
Accepts a `Vec<Route>` of items to read.
Returns `Vec<Option<RbxInstance>>`, in the same order as the request.
### POST `/write`
Accepts a `Vec<{ Route, RbxInstance }>` of items to write.
I imagine that the `Name` attribute of the top-level `RbxInstance` would be ignored in favor of the route name?
## CLI
The `rojo serve` command uses three major components:
* A Virtual Filesystem (VFS), which exposes the filesystem as `VfsItem` objects
* A VFS watcher, which tracks changes to the filesystem and logs them
* An HTTP API, which exposes an interface to the Roblox Studio plugin
### Transform Plugins
Transform plugins (or filter plugins?) can interject in three places:
* Transform a `VfsItem` that's being read into an `RbxInstance` in the VFS
* Transform an `RbxInstance` that's being written into a `VfsItem` in the VFS
* Transform a file change into paths that need to be updated in the VFS watcher
The plan is to have several built-in plugins that can be rearranged/configured in project settings:
* Base plugin
* Transforms all unhandled files to/from StringValue objects
* Script plugin
* Transforms `*.lua` files to their appropriate file types
* JSON/rbxmx/rbxlx model plugin
* External binary plugin
* User passes a binary name (like `moonc`) that modifies file contents
## Roblox Studio Plugin
With the protocol version 1 change, the Roblox Studio plugin got a lot simpler. Notably, the plugin doesn't need to be aware of anything about the filesystem's semantics, which is super handy.
## Bi-directional syncing
Quenty laid out a good way to handle bi-directional syncing.
When receiving a change from the plugin:
1. Hash the new contents of the file, store it in a map from routes to hashes
2. Write the new file contents to the filesystem
3. Later down the line, receive a change event from the filesystem watcher
4. When receiving a change, if the item is in the hash map, read it and hash those contents
5. If the hash matches the last noted hash, discard the change, else continue as normal

View File

@@ -8,11 +8,9 @@
<a href="https://travis-ci.org/LPGhatguy/rojo">
<img src="https://api.travis-ci.org/LPGhatguy/rojo.svg?branch=master" alt="Travis-CI Build Status" />
</a>
<a href="https://crates.io/crates/rojo">
<img src="https://img.shields.io/crates/v/rojo.svg?label=version" alt="Latest server version" />
</a>
<a href="https://lpghatguy.github.io/rojo/0.4.x">
<img src="https://img.shields.io/badge/documentation-0.4.x-brightgreen.svg" alt="Rojo Documentation" />
<img src="https://img.shields.io/badge/latest_version-0.4.13-brightgreen.svg" alt="Current server version" />
<a href="https://lpghatguy.github.io/rojo">
<img src="https://img.shields.io/badge/documentation-website-brightgreen.svg" alt="Rojo Documentation" />
</a>
</div>
@@ -20,9 +18,7 @@
**Rojo** is a flexible multi-tool designed for creating robust Roblox projects.
It lets Roblox developers use industry-leading tools like Git and VS Code, and crucial utilities like Luacheck.
Rojo is designed for **power users** who want to use the **best tools available** for building games, libraries, and plugins.
It's designed for power users who want to use the **best tools available** for building games, libraries, and plugins.
## Features
Rojo lets you:
@@ -31,35 +27,31 @@ Rojo lets you:
* Version your place, library, or plugin using Git or another VCS
* Sync JSON-format models from the filesystem into your game
Soon, Rojo will be able to:
Later this year, Rojo will be able to:
* Sync scripts from Roblox Studio to the filesystem
* Compile MoonScript and sync it into Roblox Studio
* Sync `rbxmx` models between the filesystem and Roblox Studio
* Package projects into `rbxmx` files from the command line
## [Documentation](https://lpghatguy.github.io/rojo/0.4.x)
You can also view the documentation by browsing the [docs](https://github.com/LPGhatguy/rojo/tree/master/docs) folder of the repository, but because it uses a number of Markdown extensions, it may not be very readable.
## [Documentation Website](https://lpghatguy.github.io/rojo)
You can also view the documentation by browsing the [docs folder of the repository](https://github.com/LPGhatguy/rojo/tree/master/docs), but because it uses a number of Markdown extensions, it may not be very readable.
## Inspiration and Alternatives
## Inspiration
There are lots of other tools that sync scripts into Roblox or provide other tools for working with Roblox places.
Here are a few, if you're looking for alternatives or supplements to Rojo:
* [rbxmk by Anaminus](https://github.com/anaminus/rbxmk)
* [Rofresh by Osyris](https://github.com/osyrisrblx/rofresh)
* [RbxRefresh by Osyris](https://github.com/osyrisrblx/RbxRefresh)
* [Studio Bridge by Vocksel](https://github.com/vocksel/studio-bridge)
* [Elixir by Vocksel](https://github.com/vocksel/elixir)
* [RbxRefresh by Osyris](https://github.com/osyrisrblx/RbxRefresh)
* [RbxSync by evaera](https://github.com/evaera/RbxSync)
* [CodeSync by MemoryPenguin](https://github.com/MemoryPenguin/CodeSync)
* [rbx-exteditor by MemoryPenguin](https://github.com/MemoryPenguin/rbx-exteditor)
* [CodeSync](https://github.com/MemoryPenguin/CodeSync) and [rbx-exteditor](https://github.com/MemoryPenguin/rbx-exteditor) by [MemoryPenguin](https://github.com/MemoryPenguin)
* [rbxmk by Anaminus](https://github.com/anaminus/rbxmk)
If you use a plugin that _isn't_ Rojo for syncing code, open an issue and let me know why! I'd like Rojo to be the end-all tool so that people stop reinventing solutions to this problem.
I also have a couple tools that Rojo intends to replace:
* [rbxfs](https://github.com/LPGhatguy/rbxfs), which has been deprecated by Rojo
* [rbxpacker](https://github.com/LPGhatguy/rbxpacker), which is still useful
## Contributing
The `master` branch is a rewrite known as **Epiphany**. It includes a breaking change to the project configuration format and an infrastructure overhaul.
Pull requests are welcome!
All pull requests are run against a test suite on Travis CI. That test suite should always pass!

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

View File

@@ -1,41 +0,0 @@
<TextureAtlas imagePath="sheet.png">
<SubTexture name="grey_arrowDownGrey.png" x="78" y="498" width="15" height="10"/>
<SubTexture name="grey_arrowDownWhite.png" x="123" y="496" width="15" height="10"/>
<SubTexture name="grey_arrowUpGrey.png" x="108" y="498" width="15" height="10"/>
<SubTexture name="grey_arrowUpWhite.png" x="93" y="498" width="15" height="10"/>
<SubTexture name="grey_box.png" x="147" y="433" width="38" height="36"/>
<SubTexture name="grey_boxCheckmark.png" x="147" y="469" width="38" height="36"/>
<SubTexture name="grey_boxCross.png" x="185" y="433" width="38" height="36"/>
<SubTexture name="grey_boxTick.png" x="190" y="198" width="36" height="36"/>
<SubTexture name="grey_button00.png" x="0" y="143" width="190" height="45"/>
<SubTexture name="grey_button01.png" x="0" y="188" width="190" height="49"/>
<SubTexture name="grey_button02.png" x="0" y="98" width="190" height="45"/>
<SubTexture name="grey_button03.png" x="0" y="331" width="190" height="49"/>
<SubTexture name="grey_button04.png" x="0" y="286" width="190" height="45"/>
<SubTexture name="grey_button05.png" x="0" y="0" width="195" height="49"/>
<SubTexture name="grey_button06.png" x="0" y="49" width="191" height="49"/>
<SubTexture name="grey_button07.png" x="195" y="0" width="49" height="49"/>
<SubTexture name="grey_button08.png" x="240" y="49" width="49" height="49"/>
<SubTexture name="grey_button09.png" x="98" y="433" width="49" height="45"/>
<SubTexture name="grey_button10.png" x="191" y="49" width="49" height="49"/>
<SubTexture name="grey_button11.png" x="0" y="433" width="49" height="45"/>
<SubTexture name="grey_button12.png" x="244" y="0" width="49" height="49"/>
<SubTexture name="grey_button13.png" x="49" y="433" width="49" height="45"/>
<SubTexture name="grey_button14.png" x="0" y="384" width="190" height="49"/>
<SubTexture name="grey_button15.png" x="0" y="237" width="190" height="49"/>
<SubTexture name="grey_checkmarkGrey.png" x="99" y="478" width="21" height="20"/>
<SubTexture name="grey_checkmarkWhite.png" x="78" y="478" width="21" height="20"/>
<SubTexture name="grey_circle.png" x="185" y="469" width="36" height="36"/>
<SubTexture name="grey_crossGrey.png" x="120" y="478" width="18" height="18"/>
<SubTexture name="grey_crossWhite.png" x="190" y="318" width="18" height="18"/>
<SubTexture name="grey_panel.png" x="190" y="98" width="100" height="100"/>
<SubTexture name="grey_sliderDown.png" x="190" y="234" width="28" height="42"/>
<SubTexture name="grey_sliderEnd.png" x="138" y="478" width="8" height="10"/>
<SubTexture name="grey_sliderHorizontal.png" x="0" y="380" width="190" height="4"/>
<SubTexture name="grey_sliderLeft.png" x="0" y="478" width="39" height="31"/>
<SubTexture name="grey_sliderRight.png" x="39" y="478" width="39" height="31"/>
<SubTexture name="grey_sliderUp.png" x="190" y="276" width="28" height="42"/>
<SubTexture name="grey_sliderVertical.png" x="208" y="318" width="4" height="100"/>
<SubTexture name="grey_tickGrey.png" x="190" y="336" width="17" height="17"/>
<SubTexture name="grey_tickWhite.png" x="190" y="353" width="17" height="17"/>
</TextureAtlas>

View File

@@ -1,39 +0,0 @@
digraph G {
graph [
ranksep = "0.7",
nodesep = "1.0",
];
node [
fontname = "Hack",
shape = "record",
];
roblox_studio -> plugin [dir = "both"];
plugin -> web_server [style = "dashed", dir = "both"];
web_server -> session;
session -> rbx_session;
session -> fs_watcher;
session -> message_queue;
fs_watcher -> imfs [weight = "10"];
fs_watcher -> rbx_session [constraint = "false"];
imfs -> fs;
rbx_session -> imfs;
rbx_session -> middlewares [weight = "10"];
rbx_session -> message_queue [constraint = "false"];
plugin [label = "Studio Plugin"];
roblox_studio [label = "Roblox Studio"];
fs [label = "Filesystem"];
fs_watcher [label = "Filesystem Watcher"];
session [label = "Session"];
web_server [label = "Web API"];
imfs [label = "In-Memory Filesystem"];
rbx_session [label = "RbxSession"];
message_queue [label = "MessageQueue"];
middlewares [label = "Middlewares"];
}

View File

@@ -1,64 +0,0 @@
To use Rojo to build a place, you'll need to create a new project file, which tells Rojo how your project is structured on-disk and in Roblox.
Create a new folder, then run `rojo init` inside that folder to initialize an empty project.
```sh
mkdir my-new-project
cd my-new-project
rojo init
```
Rojo will make a small project file in your directory, named `roblox-project.json`. It'll make sure that any code in the directory `src` will get put into `ReplicatedStorage.Source`.
Speaking of, let's make sure we create a directory named `src`, and maybe a Lua file inside of it:
```sh
mkdir src
echo 'print("Hello, world!")' > src/hello.lua
```
## Building Your Place
Now that we have a project, one thing we can do is build a Roblox place file for our project. This is a great way to get started with a project quickly with no fuss.
All we have to do is call `rojo build`:
```sh
rojo build -o MyNewProject.rbxl
```
If you open `MyNewProject.rbxl` in Roblox Studio now, you should see a `Folder` containing a `ModuleScript` under `ReplicatedStorage`!
!!! info
To generate an XML place file instead, like if you're checking the place file into version control, just use `rbxlx` as the extension on the output file instead.
## Live-Syncing into Studio
Building a place file is great for the initial build, but for actively working on your place, you'll want something quicker.
In Roblox Studio, make sure the Rojo plugin is installed. If you need it, check out [the installation guide](installation) to learn how to install it.
To expose your project to the plugin, you'll need to _serve_ it from the command line:
```sh
rojo serve
```
This will start up a web server that tells Roblox Studio what instances are in your project and sends notifications if any of them change.
Note the port number, then switch into Roblox Studio and press the Rojo **Connect** button in the plugins tab. Type in the port number, if necessary, and press **Start**.
If everything went well, you should now be able to change files in the `src` directory and watch them sync into Roblox Studio in real time!
## Uploading Your Place
Aimed at teams that want serious levels of automation, Rojo can upload places to Roblox.com automatically.
You'll need an existing place on Roblox.com as well as the `.ROBLOSECURITY` cookie of an account that has write access to that place.
!!! warning
It's recommended that you set up a Roblox account dedicated to deploying your place instead of your personal account in case your security cookie is compromised.
Generating and uploading your place file is as simple as:
```sh
rojo upload --asset_id [PLACE ID] --cookie "[SECURITY COOKIE]"
```

View File

@@ -0,0 +1,77 @@
# Creating a Project
To use Rojo, you'll need to create a new project file, which tells Rojo what your project is, and how to load it into Roblox Studio.
## New Project
Create a new folder, then run `rojo init` inside that folder to initialize an empty project.
```sh
mkdir my-new-project
cd my-new-project
rojo init
```
Rojo will create an empty project file named `rojo.json` in the directory.
The default configuration doesn't do anything. We need to tell Rojo where our code is on the filesystem, and where we want to put it in the Roblox tree.
To do that, open up `rojo.json` and add an entry to the `partitions` table:
```json
{
"name": "your-project-name-here",
"servePort": 8000,
"partitions": {
"src": {
"path": "src",
"target": "ReplicatedStorage.Project"
}
}
}
```
!!! warning
Make sure that the `src` directory exists in your project, or Rojo will throw an error!
!!! warning
Any objects contained in the `target` of a partition will be destroyed by Rojo if not found on the filesystem!
A Rojo project has one or more *partitions*. Partitions define how code should be transferred between the filesystem and Roblox by mapping directories and files to Roblox objects.
Each partition has:
* A name (the key in the `partitions` object), which is used for debugging
* `path`, the location on the filesystem relative to `rojo.json`
* `target`, the location in Roblox relative to `game`
## Syncing into Studio
Once you've added your partition to the project file, you can start the Rojo dev server by running a command in your project's directory:
```sh
rojo serve
```
If your project is in the right place, Rojo will let you know that it was found and start an HTTP server that the plugin can connect to.
In Roblox Studio, open the plugins tab and find Rojo's buttons.
![Location of Rojo's plugin buttons in Roblox Studio](/images/plugin-buttons.png)
{: align="center" }
Press **Test Connection** to verify that the plugin can communicate with the dev server. Watch the Output panel for the results.
!!! info
If you see an error message, return to the previous steps and make sure that the Rojo dev server is running.
![Rojo error in Roblox Studio Output](/images/connection-error.png)
{: align="center" }
After your connection was successful, press **Sync In** to move code from the filesystem into Studio, or use **Toggle Polling** to have Rojo automatically sync in changes as they happen.
## Importing an Existing Project
Rojo will eventually support importing an existing Roblox project onto the filesystem for use with Rojo.
Rojo doesn't currently support converting an existing project or syncing files from Roblox Studio onto the filesystem. In the mean time, you can manually copy your files into the structure that Rojo expects.
Up-to-date information will be available on [issue #5](https://github.com/LPGhatguy/rojo/issues/5) as this is worked on.

View File

@@ -1,3 +1,4 @@
# Installation
Rojo has two components:
* The server, a binary written in Rust

View File

@@ -1,11 +1,6 @@
# Home
This is the documentation home for Rojo.
Available versions of these docs:
* [0.5.x](https://lpghatguy.github.io/rojo/0.5.x)
* [0.4.x](https://lpghatguy.github.io/rojo/0.4.x)
* [`master` branch](https://lpghatguy.github.io/rojo/master)
**Rojo** is a flexible multi-tool designed for creating robust Roblox projects.
This documentation is a continual work in progress. If you find any issues, please file an issue on [Rojo's issue tracker](https://github.com/LPGhatguy/rojo/issues)!
This documentation is a work in progress, and is incomplete.

View File

@@ -1,59 +0,0 @@
Rojo underwent a large refactor during most of 2018 to enable a bunch of new features and lay groundwork for lots more in 2019. As such, Rojo **0.5.x** projects are not compatible with Rojo **0.4.x** projects.
## Supporting Both 0.4.x and 0.5.x
Rojo 0.5.x uses a different name for its project format. While 0.4.x used `rojo.json`, 0.5.x uses `roblox-project.json`, which allows them to coexist.
If you aren't sure about upgrading or want to upgrade gradually, it's possible to keep both files in the same project without causing problems.
## Upgrading Your Project File
Project files in 0.5.x are more explicit and flexible than they were in 0.4.x. Project files can now describe models and plugins in addition to places.
This new project file format also guards against two of the biggest pitfalls when writing a config file:
* Using a service as a partition target directly, which often wiped away extra instances
* Defining two partitions that overlapped, which made Rojo act unpredictably
The biggest change is that the `partitions` field has been replaced with a new field, `tree`, that describes the entire hierarchy of your project from the top-down.
A project for 0.4.x that syncs from the `src` directory into `ReplicatedStorage.Source` would look like this:
```json
{
"name": "Rojo 0.4.x Example",
"partitions": {
"path": "src",
"target": "ReplicatedStorage.Source"
}
}
```
In 0.5.x, the project format is more explicit:
```json
{
"name": "Rojo 0.5.x Example",
"tree": {
"$className": "DataModel",
"ReplicatedStorage": {
"$className": "ReplicatedStorage",
"Source": {
"$path": "src"
}
}
}
}
```
For each object in the tree, we define *metadata* and *children*.
Metadata begins with a dollar sign (`$`), like `$className`. This is so that children and metadata can coexist without creating too many nested layers.
All other values are considered children, where the key is the instance's name, and the value is an object, repeating the process.
## Migrating `.model.json` Files
No upgrade path yet, stay tuned.
## Migrating Unknown Files
If you used Rojo to sync in files as `StringValue` objects, you'll need to make sure those files end with the `txt` extension to preserve this in Rojo 0.5.x.
Unknown files are now ignored in Rojo instead of being converted to `StringValue` objects.

View File

@@ -1,24 +1,21 @@
# Sync Details
This page aims to describe how Rojo turns files on the filesystem into Roblox objects.
## Overview
| File Name | Instance Type |
| -------------- | ------------------- |
| any directory | `Folder` |
| `*.server.lua` | `Script` |
| `*.client.lua` | `LocalScript` |
| `*.lua` | `ModuleScript` |
| `*.csv` | `LocalizationTable` |
| `*.txt` | `StringValue` |
## Folders
Any directory on the filesystem will turn into a `Folder` instance unless it contains an 'init' script, described below.
Any directory on the filesystem will turn into a `Folder` instance in Roblox, unless that folder matches the name of a service or other existing instance. In those cases, that instance will be preserved.
## Scripts
The default script type in Rojo projects is `ModuleScript`, since most scripts in well-structued Roblox projects will be modules.
Rojo can represent `ModuleScript`, `Script`, and `LocalScript` objects. The default script type is `ModuleScript`, since most scripts in well-structued Roblox projects will be modules.
If a directory contains a file named `init.server.lua`, `init.client.lua`, or `init.lua`, that folder will be transformed into a `*Script` instance with the contents of the 'init' file. This can be used to create scripts inside of scripts.
| File Name | Instance Type |
| -------------- | -------------- |
| `*.server.lua` | `Script` |
| `*.client.lua` | `LocalScript` |
| `*.lua` | `ModuleScript` |
For example, these files:
If a directory contains a file named `init.server.lua`, `init.client.lua`, or `init.lua`, that folder will be transformed into a `*Script` instance with the conents of the `init` file. This can be used to create scripts inside of scripts.
For example, this file tree:
* my-game
* init.client.lua
@@ -28,8 +25,41 @@ Will turn into these instances in Roblox:
![Example of Roblox instances](/images/sync-example.png)
## Localization Tables
Any CSV files are transformed into `LocalizationTable` instances. Rojo expects these files to follow the same format that Roblox does when importing and exporting localization information.
## Models
Rojo supports a JSON model format for representing simple models. It's designed for instance types like `BindableEvent` or `Value` objects, and is not suitable for larger models.
## Plain Text Files
Plain text files (`.txt`) files are transformed into `StringValue` instances. This is useful for bringing in text data that can be read by scripts at runtime.
Rojo JSON models are stored in `.model.json` files.
Starting in Rojo version **0.4.10**, model files named `init.model.json` that are located in folders will replace that folder, much like Rojo's `init.lua` support. This can be useful to version instances like `Tool` that tend to contain several instances as well as one or more scripts.
!!! info
In the future, Rojo will support `.rbxmx` models. See [issue #7](https://github.com/LPGhatguy/rojo/issues/7) for more details and updates on this feature.
!!! warning
Prior to Rojo version **0.4.9**, the `Properties` and `Children` properties are required on all instances in JSON models!
JSON model files are fairly strict; any syntax errors will cause the model to fail to sync! They look like this:
`hello.model.json`
```json
{
"Name": "hello",
"ClassName": "Model",
"Children": [
{
"Name": "Some Part",
"ClassName": "Part"
},
{
"Name": "Some StringValue",
"ClassName": "StringValue",
"Properties": {
"Value": {
"Type": "String",
"Value": "Hello, world!"
}
}
}
]
}
```

View File

@@ -1,3 +1,4 @@
# Why Rojo?
There are a number of existing plugins for Roblox that move code from the filesystem into Roblox.
Besides Rojo, there is:

View File

@@ -1,25 +0,0 @@
#!/bin/sh
# Kludged documentation generator to support multiple versions.
# Make sure the `site` folder is a checkout of this repository's `gh-pages`
# branch.
# To use, copy this file to `generate-docs.run` so that Git will leave it alone,
# then run `generate-docs.run` in the root of the repository.
set -e
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "Building 0.4.x"
git checkout v0.4.x
git pull
mkdocs build --site-dir site/0.4.x
echo "Building master"
git checkout master
mkdocs build --site-dir site/master
echo "Building 0.5.x"
mkdocs build --site-dir site/0.5.x
git checkout "$CURRENT_BRANCH"

View File

@@ -1,4 +1,5 @@
site_name: Rojo Documentation
site_url: https://lpghatguy.github.io/rojo/
repo_name: LPGhatguy/rojo
repo_url: https://github.com/LPGhatguy/rojo
@@ -8,14 +9,13 @@ theme:
primary: 'Red'
accent: 'Red'
nav:
pages:
- Home: index.md
- Why Rojo?: why-rojo.md
- Getting Started:
- Installation: getting-started/installation.md
- Creating a Place with Rojo: getting-started/creating-a-place.md
- Creating a Project: getting-started/creating-a-project.md
- Sync Details: sync-details.md
- Migrating from 0.4.x to 0.5.x: migrating-to-epiphany.md
markdown_extensions:
- attr_list

4
plugin/.editorconfig Normal file
View File

@@ -0,0 +1,4 @@
[*.lua]
indent_style = tab
trim_trailing_whitespace = true
insert_final_newline = true

View File

@@ -39,10 +39,6 @@ stds.testez = {
ignore = {
"212", -- unused arguments
"421", -- shadowing local variable
"422", -- shadowing argument
"431", -- shadowing upvalue
"432", -- shadowing upvalue argument
}
std = "lua51+roblox"

View File

@@ -1,5 +0,0 @@
# Rojo Plugin
This is the source to the Rojo Roblox Studio plugin.
Documentation is WIP.

View File

@@ -1,37 +0,0 @@
--[[
Loads the Rojo plugin and all of its dependencies.
]]
local function loadEnvironment()
-- If you add any dependencies, add them to this table so they'll be loaded!
local LOAD_MODULES = {
{"src", "Rojo"},
{"modules/promise/lib", "Promise"},
{"modules/testez/lib", "TestEZ"},
}
-- This makes sure we can load Lemur and other libraries that depend on init.lua
package.path = package.path .. ";?/init.lua"
-- If this fails, make sure you've run `lua bin/install-dependencies.lua` first!
local lemur = require("modules.lemur")
-- Create a virtual Roblox tree
local habitat = lemur.Habitat.new()
-- We'll put all of our library code and dependencies here
local modules = lemur.Instance.new("Folder")
modules.Name = "Modules"
modules.Parent = habitat.game:GetService("ReplicatedStorage")
-- Load all of the modules specified above
for _, module in ipairs(LOAD_MODULES) do
local container = habitat:loadFromFs(module[1])
container.Name = module[2]
container.Parent = modules
end
return habitat, modules
end
return loadEnvironment

View File

@@ -1,21 +0,0 @@
{
"name": "Rojo",
"tree": {
"$className": "Folder",
"Plugin": {
"$path": "src"
},
"Roact": {
"$path": "modules/roact/lib"
},
"Rodux": {
"$path": "modules/rodux/lib"
},
"RoactRodux": {
"$path": "modules/roact-rodux/lib"
},
"Promise": {
"$path": "modules/promise/lib"
}
}
}

View File

@@ -4,31 +4,31 @@
"partitions": {
"plugin": {
"path": "src",
"target": "ReplicatedStorage.Rojo.Plugin"
"target": "ReplicatedStorage.Rojo.plugin"
},
"modules/roact": {
"path": "modules/roact/lib",
"target": "ReplicatedStorage.Rojo.Roact"
"target": "ReplicatedStorage.Rojo.modules.Roact"
},
"modules/rodux": {
"path": "modules/rodux/lib",
"target": "ReplicatedStorage.Rojo.Rodux"
"target": "ReplicatedStorage.Rojo.modules.Rodux"
},
"modules/roact-rodux": {
"path": "modules/roact-rodux/lib",
"target": "ReplicatedStorage.Rojo.RoactRodux"
"target": "ReplicatedStorage.Rojo.modules.RoactRodux"
},
"modules/promise": {
"path": "modules/promise/lib",
"target": "ReplicatedStorage.Rojo.Promise"
"target": "ReplicatedStorage.Rojo.modules.Promise"
},
"modules/testez": {
"path": "modules/testez/lib",
"target": "ReplicatedStorage.TestEZ"
},
"tests": {
"path": "testBootstrap.server.lua",
"target": "TestService.testBootstrap"
"path": "tests",
"target": "TestService"
}
}
}

View File

@@ -1,15 +0,0 @@
local loadEnvironment = require("loadEnvironment")
local testPath = assert((...), "Please specify a path to a test file.")
local habitat = loadEnvironment()
local testModule = habitat:loadFromFs(testPath)
if testModule == nil then
error("Couldn't find test file at " .. testPath)
end
print("Starting test module.")
habitat:require(testModule)

View File

@@ -2,16 +2,68 @@
Loads our library and all of its dependencies, then runs tests using TestEZ.
]]
local loadEnvironment = require("loadEnvironment")
-- If you add any dependencies, add them to this table so they'll be loaded!
local LOAD_MODULES = {
{"src", "Plugin"},
{"modules/testez/lib", "TestEZ"},
}
local habitat, modules = loadEnvironment()
-- This makes sure we can load Lemur and other libraries that depend on init.lua
package.path = package.path .. ";?/init.lua"
-- If this fails, make sure you've run `lua bin/install-dependencies.lua` first!
local lemur = require("modules.lemur")
--[[
Collapses ModuleScripts named 'init' into their parent folders.
This is the same result as the collapsing mechanism from Rojo.
]]
local function collapse(root)
local init = root:FindFirstChild("init")
if init then
init.Name = root.Name
init.Parent = root.Parent
for _, child in ipairs(root:GetChildren()) do
child.Parent = init
end
root:Destroy()
root = init
end
for _, child in ipairs(root:GetChildren()) do
if child:IsA("Folder") then
collapse(child)
end
end
return root
end
-- Create a virtual Roblox tree
local habitat = lemur.Habitat.new()
-- We'll put all of our library code and dependencies here
local Root = lemur.Instance.new("Folder")
Root.Name = "Root"
-- Load all of the modules specified above
for _, module in ipairs(LOAD_MODULES) do
local container = lemur.Instance.new("Folder", Root)
container.Name = module[2]
habitat:loadFromFs(module[1], container)
end
collapse(Root)
-- Load TestEZ and run our tests
local TestEZ = habitat:require(modules.TestEZ)
local TestEZ = habitat:require(Root.TestEZ)
local results = TestEZ.TestBootstrap:run({modules.Rojo}, TestEZ.Reporters.TextReporter)
local results = TestEZ.TestBootstrap:run(Root.Plugin, TestEZ.Reporters.TextReporter)
-- Did something go wrong?
if results.failureCount > 0 then
os.exit(1)
end
end

114
plugin/src/Api.lua Normal file
View File

@@ -0,0 +1,114 @@
local HttpService = game:GetService("HttpService")
local Promise = require(script.Parent.Parent.modules.Promise)
local Config = require(script.Parent.Config)
local Version = require(script.Parent.Version)
local Api = {}
Api.__index = Api
Api.Error = {
ServerIdMismatch = "ServerIdMismatch",
}
setmetatable(Api.Error, {
__index = function(_, key)
error("Invalid API.Error name " .. key, 2)
end
})
--[[
Api.connect(Http) -> Promise<Api>
Create a new Api using the given HTTP implementation.
Attempting to invoke methods on an invalid conext will throw errors!
]]
function Api.connect(http)
local context = {
http = http,
serverId = nil,
currentTime = 0,
}
setmetatable(context, Api)
return context:_start()
:andThen(function()
return context
end)
end
function Api:_start()
return self.http:get("/")
:andThen(function(response)
response = response:json()
if response.protocolVersion ~= Config.protocolVersion then
local message = (
"Found a Rojo dev server, but it's using a different protocol version, and is incompatible." ..
"\nMake sure you have matching versions of both the Rojo plugin and server!" ..
"\n\nYour client is version %s, with protocol version %s. It expects server version %s." ..
"\nYour server is version %s, with protocol version %s." ..
"\n\nGo to https://github.com/LPGhatguy/rojo for more details."
):format(
Version.display(Config.version), Config.protocolVersion,
Config.expectedApiVersionString,
response.serverVersion, response.protocolVersion
)
return Promise.reject(message)
end
self.serverId = response.serverId
self.currentTime = response.currentTime
end)
end
function Api:getInfo()
return self.http:get("/")
:andThen(function(response)
response = response:json()
if response.serverId ~= self.serverId then
return Promise.reject(Api.Error.ServerIdMismatch)
end
return response
end)
end
function Api:read(paths)
local body = HttpService:JSONEncode(paths)
return self.http:post("/read", body)
:andThen(function(response)
response = response:json()
if response.serverId ~= self.serverId then
return Promise.reject(Api.Error.ServerIdMismatch)
end
return response.items
end)
end
function Api:getChanges()
local url = ("/changes/%f"):format(self.currentTime)
return self.http:get(url)
:andThen(function(response)
response = response:json()
if response.serverId ~= self.serverId then
return Promise.reject(Api.Error.ServerIdMismatch)
end
self.currentTime = response.currentTime
return response.changes
end)
end
return Api

View File

@@ -1,145 +0,0 @@
local Promise = require(script.Parent.Parent.Promise)
local Config = require(script.Parent.Config)
local Version = require(script.Parent.Version)
local Http = require(script.Parent.Http)
local HttpError = require(script.Parent.HttpError)
local ApiContext = {}
ApiContext.__index = ApiContext
-- TODO: Audit cases of errors and create enum values for each of them.
ApiContext.Error = {
ServerIdMismatch = "ServerIdMismatch",
}
setmetatable(ApiContext.Error, {
__index = function(_, key)
error("Invalid ApiContext.Error name " .. key, 2)
end
})
function ApiContext.new(baseUrl)
assert(type(baseUrl) == "string")
local self = {
baseUrl = baseUrl,
serverId = nil,
rootInstanceId = nil,
messageCursor = -1,
partitionRoutes = nil,
}
setmetatable(self, ApiContext)
return self
end
function ApiContext:onMessage(callback)
self.onMessageCallback = callback
end
function ApiContext:connect()
local url = ("%s/api/rojo"):format(self.baseUrl)
return Http.get(url)
:andThen(function(response)
local body = response:json()
if body.protocolVersion ~= Config.protocolVersion then
local message = (
"Found a Rojo dev server, but it's using a different protocol version, and is incompatible." ..
"\nMake sure you have matching versions of both the Rojo plugin and server!" ..
"\n\nYour client is version %s, with protocol version %s. It expects server version %s." ..
"\nYour server is version %s, with protocol version %s." ..
"\n\nGo to https://github.com/LPGhatguy/rojo for more details."
):format(
Version.display(Config.version), Config.protocolVersion,
Config.expectedApiContextVersionString,
body.serverVersion, body.protocolVersion
)
return Promise.reject(message)
end
if body.expectedPlaceIds ~= nil then
local foundId = false
for _, id in ipairs(body.expectedPlaceIds) do
if id == game.PlaceId then
foundId = true
break
end
end
if not foundId then
local idList = {}
for _, id in ipairs(body.expectedPlaceIds) do
table.insert(idList, "- " .. tostring(id))
end
local message = (
"Found a Rojo server, but its project is set to only be used with a specific list of places." ..
"\nYour place ID is %s, but needs to be one of these:" ..
"\n%s" ..
"\n\nTo change this list, edit 'servePlaceIds' in roblox-project.json"
):format(
tostring(game.PlaceId),
table.concat(idList, "\n")
)
return Promise.reject(message)
end
end
self.serverId = body.serverId
self.partitionRoutes = body.partitions
self.rootInstanceId = body.rootInstanceId
end)
end
function ApiContext:read(ids)
local url = ("%s/api/read/%s"):format(self.baseUrl, table.concat(ids, ","))
return Http.get(url)
:catch(function(err)
return Promise.reject(err)
end)
:andThen(function(response)
local body = response:json()
if body.serverId ~= self.serverId then
return Promise.reject("Server changed ID")
end
self.messageCursor = body.messageCursor
return body
end)
end
function ApiContext:retrieveMessages()
local url = ("%s/api/subscribe/%s"):format(self.baseUrl, self.messageCursor)
return Http.get(url)
:catch(function(err)
if err.type == HttpError.Error.Timeout then
return self:retrieveMessages()
end
return Promise.reject(err)
end)
:andThen(function(response)
local body = response:json()
if body.serverId ~= self.serverId then
return Promise.reject("Server changed ID")
end
self.messageCursor = body.messageCursor
return body.messages
end)
end
return ApiContext

View File

@@ -1,52 +0,0 @@
local sheetAsset = "rbxassetid://2738712459"
local Assets = {
Sprites = {
WhiteCross = {
asset = sheetAsset,
offset = Vector2.new(190, 318),
size = Vector2.new(18, 18),
},
},
Slices = {
GrayBox = {
asset = sheetAsset,
offset = Vector2.new(147, 433),
size = Vector2.new(38, 36),
center = Rect.new(8, 8, 9, 9),
},
GrayButton02 = {
asset = sheetAsset,
offset = Vector2.new(0, 98),
size = Vector2.new(190, 45),
center = Rect.new(16, 16, 17, 17),
},
GrayButton07 = {
asset = sheetAsset,
offset = Vector2.new(195, 0),
size = Vector2.new(49, 49),
center = Rect.new(16, 16, 17, 17),
},
},
StartSession = "",
SessionActive = "",
Configure = "",
}
local function guardForTypos(name, map)
setmetatable(map, {
__index = function(_, key)
error(("%q is not a valid member of %s"):format(tostring(key), name), 2)
end
})
for key, child in pairs(map) do
if type(child) == "table" then
guardForTypos(("%s.%s"):format(name, key), child)
end
end
end
guardForTypos("Assets", Assets)
return Assets

View File

@@ -1,194 +0,0 @@
local Roact = require(script:FindFirstAncestor("Rojo").Roact)
local Plugin = script:FindFirstAncestor("Plugin")
local Assets = require(Plugin.Assets)
local Session = require(Plugin.Session)
local Config = require(Plugin.Config)
local Version = require(Plugin.Version)
local Logging = require(Plugin.Logging)
local DevSettings = require(Plugin.DevSettings)
local ConnectPanel = require(Plugin.Components.ConnectPanel)
local ConnectionActivePanel = require(Plugin.Components.ConnectionActivePanel)
local e = Roact.createElement
local function showUpgradeMessage(lastVersion)
local message = (
"Rojo detected an upgrade from version %s to version %s." ..
"\nMake sure you have also upgraded your server!" ..
"\n\nRojo plugin version %s is intended for use with server version %s."
):format(
Version.display(lastVersion), Version.display(Config.version),
Version.display(Config.version), Config.expectedServerVersionString
)
Logging.info(message)
end
--[[
Check if the user is using a newer version of Rojo than last time. If they
are, show them a reminder to make sure they check their server version.
]]
local function checkUpgrade(plugin)
-- When developing Rojo, there's no use in doing version checks
if DevSettings:isEnabled() then
return
end
local lastVersion = plugin:GetSetting("LastRojoVersion")
if lastVersion then
local wasUpgraded = Version.compare(Config.version, lastVersion) == 1
if wasUpgraded then
showUpgradeMessage(lastVersion)
end
end
plugin:SetSetting("LastRojoVersion", Config.version)
end
local SessionStatus = {
Disconnected = "Disconnected",
Connected = "Connected",
ConfiguringSession = "ConfiguringSession",
-- TODO: Error?
}
setmetatable(SessionStatus, {
__index = function(_, key)
error(("%q is not a valid member of SessionStatus"):format(tostring(key)), 2)
end,
})
local App = Roact.Component:extend("App")
function App:init()
self:setState({
sessionStatus = SessionStatus.Disconnected,
})
self.connectButton = nil
self.configButton = nil
self.currentSession = nil
self.displayedVersion = DevSettings:isEnabled()
and Config.codename
or Version.display(Config.version)
end
function App:render()
local children
if self.state.sessionStatus == SessionStatus.Connected then
children = {
ConnectionActivePanel = e(ConnectionActivePanel),
}
elseif self.state.sessionStatus == SessionStatus.ConfiguringSession then
children = {
ConnectPanel = e(ConnectPanel, {
startSession = function(address, port)
Logging.trace("Starting new session")
local success, session = Session.new({
address = address,
port = port,
onError = function(message)
Logging.warn("%s", tostring(message))
Logging.trace("Session terminated due to error")
self.currentSession = nil
self:setState({
sessionStatus = SessionStatus.Disconnected,
})
end
})
if success then
self.currentSession = session
self:setState({
sessionStatus = SessionStatus.Connected,
})
end
end,
cancel = function()
Logging.trace("Canceling session configuration")
self:setState({
sessionStatus = SessionStatus.Disconnected,
})
end,
}),
}
end
return e("ScreenGui", {
AutoLocalize = false,
ZIndexBehavior = Enum.ZIndexBehavior.Sibling,
}, children)
end
function App:didMount()
Logging.trace("Rojo %s initializing", self.displayedVersion)
local toolbar = self.props.plugin:CreateToolbar("Rojo " .. self.displayedVersion)
self.connectButton = toolbar:CreateButton(
"Connect",
"Connect to a running Rojo session",
Assets.StartSession)
self.connectButton.ClickableWhenViewportHidden = false
self.connectButton.Click:Connect(function()
checkUpgrade(self.props.plugin)
if self.state.sessionStatus == SessionStatus.Connected then
Logging.trace("Disconnecting session")
self.currentSession:disconnect()
self.currentSession = nil
self:setState({
sessionStatus = SessionStatus.Disconnected,
})
Logging.trace("Session terminated by user")
elseif self.state.sessionStatus == SessionStatus.Disconnected then
Logging.trace("Starting session configuration")
self:setState({
sessionStatus = SessionStatus.ConfiguringSession,
})
elseif self.state.sessionStatus == SessionStatus.ConfiguringSession then
Logging.trace("Canceling session configuration")
self:setState({
sessionStatus = SessionStatus.Disconnected,
})
end
end)
self.configButton = toolbar:CreateButton(
"Configure",
"Configure the Rojo plugin",
Assets.Configure)
self.configButton.ClickableWhenViewportHidden = false
self.configButton.Click:Connect(function()
self.configButton:SetActive(false)
end)
end
function App:didUpdate()
local connectActive = self.state.sessionStatus == SessionStatus.ConfiguringSession
or self.state.sessionStatus == SessionStatus.Connected
self.connectButton:SetActive(connectActive)
if self.state.sessionStatus == SessionStatus.Connected then
self.connectButton.Icon = Assets.SessionActive
else
self.connectButton.Icon = Assets.StartSession
end
end
return App

View File

@@ -1,243 +0,0 @@
local Rojo = script:FindFirstAncestor("Rojo")
local Plugin = Rojo.Plugin
local Roact = require(Rojo.Roact)
local Config = require(Plugin.Config)
local Assets = require(Plugin.Assets)
local FitList = require(Plugin.Components.FitList)
local FitText = require(Plugin.Components.FitText)
local FormButton = require(Plugin.Components.FormButton)
local FormTextInput = require(Plugin.Components.FormTextInput)
local WhiteCross = Assets.Sprites.WhiteCross
local GrayBox = Assets.Slices.GrayBox
local e = Roact.createElement
local TEXT_COLOR = Color3.new(0.05, 0.05, 0.05)
local FORM_TEXT_SIZE = 20
local ConnectPanel = Roact.Component:extend("ConnectPanel")
function ConnectPanel:init()
self.labelSizes = {}
self.labelSize, self.setLabelSize = Roact.createBinding(Vector2.new())
self:setState({
address = Config.defaultHost,
port = Config.defaultPort,
})
end
function ConnectPanel:updateLabelSize(name, size)
self.labelSizes[name] = size
local x = 0
local y = 0
for _, size in pairs(self.labelSizes) do
x = math.max(x, size.X)
y = math.max(y, size.Y)
end
self.setLabelSize(Vector2.new(x, y))
end
function ConnectPanel:render()
local startSession = self.props.startSession
local cancel = self.props.cancel
return e(FitList, {
containerKind = "ImageLabel",
containerProps = {
Image = GrayBox.asset,
ImageRectOffset = GrayBox.offset,
ImageRectSize = GrayBox.size,
ScaleType = Enum.ScaleType.Slice,
SliceCenter = GrayBox.center,
BackgroundTransparency = 1,
Position = UDim2.new(0.5, 0, 0.5, 0),
AnchorPoint = Vector2.new(0.5, 0.5),
},
layoutProps = {
HorizontalAlignment = Enum.HorizontalAlignment.Center,
},
}, {
Head = e("Frame", {
LayoutOrder = 1,
Size = UDim2.new(1, 0, 0, 36),
BackgroundTransparency = 1,
}, {
Padding = e("UIPadding", {
PaddingTop = UDim.new(0, 8),
PaddingBottom = UDim.new(0, 8),
PaddingLeft = UDim.new(0, 8),
PaddingRight = UDim.new(0, 8),
}),
Title = e("TextLabel", {
Font = Enum.Font.SourceSansBold,
TextSize = 22,
Text = "Start New Rojo Session",
Size = UDim2.new(1, 0, 1, 0),
TextXAlignment = Enum.TextXAlignment.Left,
BackgroundTransparency = 1,
TextColor3 = TEXT_COLOR,
}),
Close = e("ImageButton", {
Image = WhiteCross.asset,
ImageRectOffset = WhiteCross.offset,
ImageRectSize = WhiteCross.size,
Size = UDim2.new(0, 18, 0, 18),
Position = UDim2.new(1, 0, 0.5, 0),
AnchorPoint = Vector2.new(1, 0.5),
ImageColor3 = TEXT_COLOR,
BackgroundTransparency = 1,
[Roact.Event.Activated] = function()
cancel()
end,
}),
}),
Border = e("Frame", {
BorderSizePixel = 0,
BackgroundColor3 = Color3.new(0.7, 0.7, 0.7),
Size = UDim2.new(1, -4, 0, 2),
LayoutOrder = 2,
}),
Body = e(FitList, {
containerProps = {
BackgroundTransparency = 1,
LayoutOrder = 3,
},
layoutProps = {
Padding = UDim.new(0, 8),
},
paddingProps = {
PaddingTop = UDim.new(0, 8),
PaddingBottom = UDim.new(0, 8),
PaddingLeft = UDim.new(0, 8),
PaddingRight = UDim.new(0, 8),
},
}, {
Address = e(FitList, {
containerProps = {
LayoutOrder = 1,
BackgroundTransparency = 1,
},
layoutProps = {
FillDirection = Enum.FillDirection.Horizontal,
Padding = UDim.new(0, 8),
},
}, {
Label = e(FitText, {
MinSize = Vector2.new(0, 24),
Kind = "TextLabel",
LayoutOrder = 1,
BackgroundTransparency = 1,
TextXAlignment = Enum.TextXAlignment.Left,
Font = Enum.Font.SourceSansBold,
TextSize = FORM_TEXT_SIZE,
Text = "Address",
TextColor3 = TEXT_COLOR,
[Roact.Change.AbsoluteSize] = function(rbx)
self:updateLabelSize("address", rbx.AbsoluteSize)
end,
}, {
Sizing = e("UISizeConstraint", {
MinSize = self.labelSize,
}),
}),
Input = e(FormTextInput, {
layoutOrder = 2,
size = UDim2.new(0, 300, 0, 24),
value = self.state.address,
onValueChange = function(newValue)
self:setState({
address = newValue,
})
end,
}),
}),
Port = e(FitList, {
containerProps = {
LayoutOrder = 2,
BackgroundTransparency = 1,
},
layoutProps = {
FillDirection = Enum.FillDirection.Horizontal,
Padding = UDim.new(0, 8),
},
}, {
Label = e(FitText, {
MinSize = Vector2.new(0, 24),
Kind = "TextLabel",
LayoutOrder = 1,
BackgroundTransparency = 1,
TextXAlignment = Enum.TextXAlignment.Left,
Font = Enum.Font.SourceSansBold,
TextSize = FORM_TEXT_SIZE,
Text = "Port",
TextColor3 = TEXT_COLOR,
[Roact.Change.AbsoluteSize] = function(rbx)
self:updateLabelSize("port", rbx.AbsoluteSize)
end,
}, {
Sizing = e("UISizeConstraint", {
MinSize = self.labelSize,
}),
}),
Input = e(FormTextInput, {
layoutOrder = 2,
size = UDim2.new(0, 300, 0, 24),
value = self.state.port,
onValueChange = function(newValue)
self:setState({
port = newValue,
})
end,
}),
}),
Buttons = e(FitList, {
containerProps = {
LayoutOrder = 3,
BackgroundTransparency = 1,
},
layoutProps = {
FillDirection = Enum.FillDirection.Horizontal,
Padding = UDim.new(0, 8),
},
}, {
e(FormButton, {
text = "Start",
onClick = function()
if startSession ~= nil then
startSession(self.state.address, self.state.port)
end
end,
}),
e(FormButton, {
text = "Cancel",
onClick = function()
if cancel ~= nil then
cancel()
end
end,
}),
})
})
})
end
return ConnectPanel

View File

@@ -1,39 +0,0 @@
local Roact = require(script:FindFirstAncestor("Rojo").Roact)
local Assets = require(script.Parent.Parent.Assets)
local FitList = require(script.Parent.FitList)
local FitText = require(script.Parent.FitText)
local e = Roact.createElement
local GrayBox = Assets.Slices.GrayBox
local ConnectionActivePanel = Roact.Component:extend("ConnectionActivePanel")
function ConnectionActivePanel:render()
return e(FitList, {
containerKind = "ImageButton",
containerProps = {
Image = GrayBox.asset,
ImageRectOffset = GrayBox.offset,
ImageRectSize = GrayBox.size,
SliceCenter = GrayBox.center,
ScaleType = Enum.ScaleType.Slice,
BackgroundTransparency = 1,
Position = UDim2.new(0.5, 0, 0, 0),
AnchorPoint = Vector2.new(0.5, 0),
},
}, {
Text = e(FitText, {
Padding = Vector2.new(12, 6),
Font = Enum.Font.SourceSans,
TextSize = 18,
Text = "Rojo Connected",
TextColor3 = Color3.new(0.05, 0.05, 0.05),
BackgroundTransparency = 1,
}),
})
end
return ConnectionActivePanel

View File

@@ -1,50 +0,0 @@
local Roact = require(script:FindFirstAncestor("Rojo").Roact)
local Dictionary = require(script.Parent.Parent.Dictionary)
local e = Roact.createElement
local FitList = Roact.Component:extend("FitList")
function FitList:init()
self.sizeBinding, self.setSize = Roact.createBinding(UDim2.new())
end
function FitList:render()
local containerKind = self.props.containerKind or "Frame"
local containerProps = self.props.containerProps
local layoutProps = self.props.layoutProps
local paddingProps = self.props.paddingProps
local padding
if paddingProps ~= nil then
padding = e("UIPadding", paddingProps)
end
local children = Dictionary.merge(self.props[Roact.Children], {
["$Layout"] = e("UIListLayout", Dictionary.merge({
SortOrder = Enum.SortOrder.LayoutOrder,
[Roact.Change.AbsoluteContentSize] = function(instance)
local size = instance.AbsoluteContentSize
if paddingProps ~= nil then
size = size + Vector2.new(
paddingProps.PaddingLeft.Offset + paddingProps.PaddingRight.Offset,
paddingProps.PaddingTop.Offset + paddingProps.PaddingBottom.Offset)
end
self.setSize(UDim2.new(0, size.X, 0, size.Y))
end,
}, layoutProps)),
["$Padding"] = padding,
})
local fullContainerProps = Dictionary.merge(containerProps, {
Size = self.sizeBinding,
})
return e(containerKind, fullContainerProps, children)
end
return FitList

View File

@@ -1,52 +0,0 @@
local TextService = game:GetService("TextService")
local Roact = require(script:FindFirstAncestor("Rojo").Roact)
local Dictionary = require(script.Parent.Parent.Dictionary)
local e = Roact.createElement
local FitText = Roact.Component:extend("FitText")
function FitText:init()
self.sizeBinding, self.setSize = Roact.createBinding(UDim2.new())
end
function FitText:render()
local kind = self.props.Kind or "TextLabel"
local containerProps = Dictionary.merge(self.props, {
Kind = Dictionary.None,
Padding = Dictionary.None,
MinSize = Dictionary.None,
Size = self.sizeBinding
})
return e(kind, containerProps)
end
function FitText:didMount()
self:updateTextMeasurements()
end
function FitText:didUpdate()
self:updateTextMeasurements()
end
function FitText:updateTextMeasurements()
local minSize = self.props.MinSize or Vector2.new(0, 0)
local padding = self.props.Padding or Vector2.new(0, 0)
local text = self.props.Text or ""
local font = self.props.Font or Enum.Font.Legacy
local textSize = self.props.TextSize or 12
local measuredText = TextService:GetTextSize(text, textSize, font, Vector2.new(9e6, 9e6))
local totalSize = UDim2.new(
0, math.max(minSize.X, padding.X * 2 + measuredText.X),
0, math.max(minSize.Y, padding.Y * 2 + measuredText.Y))
self.setSize(totalSize)
end
return FitText

View File

@@ -1,49 +0,0 @@
local Rojo = script:FindFirstAncestor("Rojo")
local Plugin = Rojo.Plugin
local Roact = require(Rojo.Roact)
local Assets = require(Plugin.Assets)
local FitList = require(Plugin.Components.FitList)
local FitText = require(Plugin.Components.FitText)
local e = Roact.createElement
local GrayButton07 = Assets.Slices.GrayButton07
local function FormButton(props)
local text = props.text
local layoutOrder = props.layoutOrder
local onClick = props.onClick
return e(FitList, {
containerKind = "ImageButton",
containerProps = {
LayoutOrder = layoutOrder,
BackgroundTransparency = 1,
Image = GrayButton07.asset,
ImageRectOffset = GrayButton07.offset,
ImageRectSize = GrayButton07.size,
ScaleType = Enum.ScaleType.Slice,
SliceCenter = GrayButton07.center,
[Roact.Event.Activated] = function()
if onClick ~= nil then
onClick()
end
end,
},
}, {
Text = e(FitText, {
Kind = "TextLabel",
Text = text,
TextSize = 22,
Font = Enum.Font.SourceSansBold,
Padding = Vector2.new(14, 6),
TextColor3 = Color3.new(0.05, 0.05, 0.05),
BackgroundTransparency = 1,
}),
})
end
return FormButton

View File

@@ -1,47 +0,0 @@
local Rojo = script:FindFirstAncestor("Rojo")
local Plugin = Rojo.Plugin
local Roact = require(Rojo.Roact)
local Assets = require(Plugin.Assets)
local e = Roact.createElement
local GrayBox = Assets.Slices.GrayBox
local function FormTextInput(props)
local value = props.value
local onValueChange = props.onValueChange
local layoutOrder = props.layoutOrder
local size = props.size
return e("ImageLabel", {
LayoutOrder = layoutOrder,
Image = GrayBox.asset,
ImageRectOffset = GrayBox.offset,
ImageRectSize = GrayBox.size,
ScaleType = Enum.ScaleType.Slice,
SliceCenter = GrayBox.center,
Size = size,
BackgroundTransparency = 1,
}, {
InputInner = e("TextBox", {
BackgroundTransparency = 1,
Size = UDim2.new(1, -8, 1, -8),
Position = UDim2.new(0.5, 0, 0.5, 0),
AnchorPoint = Vector2.new(0.5, 0.5),
Font = Enum.Font.SourceSans,
ClearTextOnFocus = false,
TextXAlignment = Enum.TextXAlignment.Left,
TextSize = 20,
Text = value,
TextColor3 = Color3.new(0.05, 0.05, 0.05),
[Roact.Change.Text] = function(rbx)
onValueChange(rbx.Text)
end,
}),
})
end
return FormTextInput

View File

@@ -1,8 +1,12 @@
return {
codename = "Epiphany",
version = {0, 5, 0, "-alpha.0"},
expectedServerVersionString = "0.5.0 or newer",
protocolVersion = 2,
defaultHost = "localhost",
defaultPort = 34872,
}
pollingRate = 0.3,
version = {0, 4, 13},
expectedServerVersionString = "0.4.x",
protocolVersion = 1,
icons = {
syncIn = "rbxassetid://1820320573",
togglePolling = "rbxassetid://1820320064",
testConnection = "rbxassetid://1820320989",
},
dev = false,
}

View File

@@ -0,0 +1,7 @@
return function()
local Config = require(script.Parent.Config)
it("should have 'dev' disabled", function()
expect(Config.dev).to.equal(false)
end)
end

View File

@@ -1,59 +0,0 @@
local Config = require(script.Parent.Config)
local function getValueContainer()
return game:FindFirstChild("RojoDev-" .. Config.codename)
end
local valueContainer = getValueContainer()
local function getValue(name)
if valueContainer == nil then
return nil
end
local valueObject = valueContainer:FindFirstChild(name)
if valueObject == nil then
return nil
end
return valueObject.Value
end
local function setValue(name, kind, value)
local object = valueContainer:FindFirstChild(name)
if object == nil then
object = Instance.new(kind)
object.Name = name
object.Parent = valueContainer
end
object.Value = value
end
local function createAllValues()
valueContainer = getValueContainer()
if valueContainer == nil then
valueContainer = Instance.new("Folder")
valueContainer.Name = "RojoDev-" .. Config.codename
valueContainer.Parent = game
end
setValue("LogLevel", "IntValue", getValue("LogLevel") or 2)
end
_G[("ROJO_%s_DEV_CREATE"):format(Config.codename:upper())] = createAllValues
local DevSettings = {}
function DevSettings:isEnabled()
return valueContainer ~= nil
end
function DevSettings:getLogLevel()
return getValue("LogLevel")
end
return DevSettings

View File

@@ -1,33 +0,0 @@
--[[
This is a placeholder module waiting for Cryo to become available.
]]
local None = newproxy(true)
getmetatable(None).__tostring = function()
return "None"
end
local function merge(...)
local output = {}
for i = 1, select("#", ...) do
local source = select(i, ...)
if source ~= nil then
for key, value in pairs(source) do
if value == None then
output[key] = nil
else
output[key] = value
end
end
end
end
return output
end
return {
None = None,
merge = merge,
}

View File

@@ -1,75 +1,68 @@
local HttpService = game:GetService("HttpService")
local Promise = require(script.Parent.Parent.Promise)
local HTTP_DEBUG = false
local Promise = require(script.Parent.Parent.modules.Promise)
local Logging = require(script.Parent.Logging)
local HttpError = require(script.Parent.HttpError)
local HttpResponse = require(script.Parent.HttpResponse)
local lastRequestId = 0
local function dprint(...)
if HTTP_DEBUG then
print(...)
end
end
-- TODO: Factor out into separate library, especially error handling
local Http = {}
Http.__index = Http
function Http.get(url)
local requestId = lastRequestId + 1
lastRequestId = requestId
function Http.new(baseUrl)
assert(type(baseUrl) == "string", "Http.new needs a baseUrl!")
Logging.trace("GET(%d) %s", requestId, url)
local http = {
baseUrl = baseUrl
}
setmetatable(http, Http)
return http
end
function Http:get(endpoint)
dprint("\nGET", endpoint)
return Promise.new(function(resolve, reject)
coroutine.wrap(function()
local success, response = pcall(function()
return HttpService:RequestAsync({
Url = url,
Method = "GET",
})
spawn(function()
local ok, result = pcall(function()
return HttpService:GetAsync(self.baseUrl .. endpoint, true)
end)
if success then
Logging.trace("Request %d success: status code %s", requestId, response.StatusCode)
resolve(HttpResponse.fromRobloxResponse(response))
if ok then
dprint("\t", result, "\n")
resolve(HttpResponse.new(result))
else
Logging.trace("Request %d failure: %s", requestId, response)
reject(HttpError.fromErrorString(response))
reject(HttpError.fromErrorString(result))
end
end)()
end)
end)
end
function Http.post(url, body)
local requestId = lastRequestId + 1
lastRequestId = requestId
Logging.trace("POST(%d) %s\n%s", requestId, url, body)
function Http:post(endpoint, body)
dprint("\nPOST", endpoint)
dprint(body)
return Promise.new(function(resolve, reject)
coroutine.wrap(function()
local success, response = pcall(function()
return HttpService:RequestAsync({
Url = url,
Method = "POST",
Body = body,
})
spawn(function()
local ok, result = pcall(function()
return HttpService:PostAsync(self.baseUrl .. endpoint, body)
end)
if success then
Logging.trace("Request %d success: status code %s", requestId, response.StatusCode)
resolve(HttpResponse.fromRobloxResponse(response))
if ok then
dprint("\t", result, "\n")
resolve(HttpResponse.new(result))
else
Logging.trace("Request %d failure: %s", requestId, response)
reject(HttpError.fromErrorString(response))
reject(HttpError.fromErrorString(result))
end
end)()
end)
end)
end
function Http.jsonEncode(object)
return HttpService:JSONEncode(object)
end
function Http.jsonDecode(source)
return HttpService:JSONDecode(source)
end
return Http
return Http

View File

@@ -1,5 +1,3 @@
local Logging = require(script.Parent.Logging)
local HttpError = {}
HttpError.__index = HttpError
@@ -9,23 +7,14 @@ HttpError.Error = {
"Check your game settings, located in the 'Home' tab of Studio.",
},
ConnectFailed = {
message = "Couldn't connect to the Rojo server.\n" ..
"Make sure the server is running -- use 'rojo serve' to run it!",
},
Timeout = {
message = "Request timed out.",
message = "Rojo plugin couldn't connect to the Rojo server.\n" ..
"Make sure the server is running -- use 'Rojo serve' to run it!",
},
Unknown = {
message = "Unknown error: {{message}}",
message = "Rojo encountered an unknown error: {{message}}",
},
}
setmetatable(HttpError.Error, {
__index = function(_, key)
error(("%q is not a valid member of HttpError.Error"):format(tostring(key)), 2)
end,
})
function HttpError.new(type, extraMessage)
extraMessage = extraMessage or ""
local message = type.message:gsub("{{message}}", extraMessage)
@@ -47,26 +36,25 @@ end
--[[
This method shouldn't have to exist. Ugh.
]]
function HttpError.fromErrorString(message)
local lower = message:lower()
function HttpError.fromErrorString(err)
err = err:lower()
if lower:find("^http requests are not enabled") then
if err:find("^http requests are not enabled") then
return HttpError.new(HttpError.Error.HttpNotEnabled)
end
if lower:find("^httperror: timedout") then
return HttpError.new(HttpError.Error.Timeout)
end
if lower:find("^httperror: connectfail") then
if err:find("^curl error") then
return HttpError.new(HttpError.Error.ConnectFailed)
end
return HttpError.new(HttpError.Error.Unknown, message)
return HttpError.new(HttpError.Error.Unknown, err)
end
function HttpError:report()
Logging.warn(self.message)
warn(self.message)
if self.type == HttpError.Error.HttpNotEnabled then
game:GetService("Selection"):Set{game:GetService("HttpService")}
end
end
return HttpError
return HttpError

View File

@@ -1,30 +1,16 @@
local HttpService = game:GetService("HttpService")
local stringTemplate = [[
HttpResponse {
code: %d
body: %s
}]]
local HttpResponse = {}
HttpResponse.__index = HttpResponse
function HttpResponse:__tostring()
return stringTemplate:format(self.code, self.body)
end
function HttpResponse.fromRobloxResponse(response)
local self = {
body = response.Body,
code = response.StatusCode,
headers = response.Headers,
function HttpResponse.new(body)
local response = {
body = body,
}
return setmetatable(self, HttpResponse)
end
setmetatable(response, HttpResponse)
function HttpResponse:isSuccess()
return self.code >= 200 and self.code < 300
return response
end
function HttpResponse:json()

View File

@@ -1,55 +0,0 @@
local DevSettings = require(script.Parent.DevSettings)
local testLogLevel = nil
local Level = {
Error = 0,
Warning = 1,
Info = 2,
Trace = 3,
}
local function getLogLevel()
if testLogLevel ~= nil then
return testLogLevel
end
local devValue = DevSettings:getLogLevel()
if devValue ~= nil then
return devValue
end
return Level.Info
end
local function addTags(tag, message)
return tag .. message:gsub("\n", "\n" .. tag)
end
local INFO_TAG = (" "):rep(15) .. "[Rojo-Info] "
local TRACE_TAG = (" "):rep(15) .. "[Rojo-Trace] "
local WARN_TAG = "[Rojo-Warn] "
local Log = {}
Log.Level = Level
function Log.trace(template, ...)
if getLogLevel() >= Level.Trace then
print(addTags(TRACE_TAG, string.format(template, ...)))
end
end
function Log.info(template, ...)
if getLogLevel() >= Level.Info then
print(addTags(INFO_TAG, string.format(template, ...)))
end
end
function Log.warn(template, ...)
if getLogLevel() >= Level.Warning then
warn(addTags(WARN_TAG, string.format(template, ...)))
end
end
return Log

View File

@@ -0,0 +1,84 @@
if not plugin then
return
end
local Plugin = require(script.Parent.Plugin)
local Config = require(script.Parent.Config)
local Version = require(script.Parent.Version)
--[[
Check if the user is using a newer version of Rojo than last time. If they
are, show them a reminder to make sure they check their server version.
]]
local function checkUpgrade()
-- When developing Rojo, there's no use in doing version checks
if Config.dev then
return
end
local lastVersion = plugin:GetSetting("LastRojoVersion")
if lastVersion then
local wasUpgraded = Version.compare(Config.version, lastVersion) == 1
if wasUpgraded then
local message = (
"\nRojo detected an upgrade from version %s to version %s." ..
"\nMake sure you have also upgraded your server!" ..
"\n\nRojo version %s is intended for use with server version %s.\n"
):format(
Version.display(lastVersion), Version.display(Config.version),
Version.display(Config.version), Config.expectedServerVersionString
)
print(message)
end
end
plugin:SetSetting("LastRojoVersion", Config.version)
end
local function main()
local pluginInstance = Plugin.new()
local displayedVersion = Config.dev and "DEV" or Version.display(Config.version)
local toolbar = plugin:CreateToolbar("Rojo Plugin " .. displayedVersion)
toolbar:CreateButton("Test Connection", "Connect to Rojo Server", Config.icons.testConnection)
.Click:Connect(function()
checkUpgrade()
pluginInstance:connect()
:catch(function(err)
warn(err)
end)
end)
local function syncIn()
checkUpgrade()
pluginInstance:syncIn()
:catch(function(err)
warn(err)
end)
end
local shortDescription = "Sync In"
local longDescription = "Sync into Roblox Studio"
toolbar:CreateButton(shortDescription, longDescription, Config.icons.syncIn).Click:Connect(syncIn)
plugin:CreatePluginAction("RojoSyncIn", shortDescription, longDescription).Triggered:Connect(syncIn)
toolbar:CreateButton("Toggle Polling", "Poll server for changes", Config.icons.togglePolling)
.Click:Connect(function()
checkUpgrade()
pluginInstance:togglePolling()
:catch(function(err)
warn(err)
end)
end)
end
main()

311
plugin/src/Plugin.lua Normal file
View File

@@ -0,0 +1,311 @@
local CoreGui = game:GetService("CoreGui")
local Promise = require(script.Parent.Parent.modules.Promise)
local Config = require(script.Parent.Config)
local Http = require(script.Parent.Http)
local Api = require(script.Parent.Api)
local Reconciler = require(script.Parent.Reconciler)
local Version = require(script.Parent.Version)
local MESSAGE_SERVER_CHANGED = "Rojo: The server has changed since the last request, reloading plugin..."
local MESSAGE_PLUGIN_CHANGED = "Rojo: Another instance of Rojo came online, unloading..."
local function collectMatch(source, pattern)
local result = {}
for match in source:gmatch(pattern) do
table.insert(result, match)
end
return result
end
local Plugin = {}
Plugin.__index = Plugin
function Plugin.new()
local address = "localhost"
local port = Config.dev and 8001 or 8000
local remote = ("http://%s:%d"):format(address, port)
local self = {
_http = Http.new(remote),
_reconciler = Reconciler.new(),
_api = nil,
_polling = false,
_syncInProgress = false,
}
setmetatable(self, Plugin)
do
local uiName = ("Rojo %s UI"):format(Version.display(Config.version))
if Config.dev then
uiName = "Rojo Dev UI"
end
-- If there's an existing Rojo UI, like from a Roblox plugin upgrade
-- that wasn't Rojo, make sure we clean it up.
local existingUi = CoreGui:FindFirstChild(uiName)
if existingUi ~= nil then
existingUi:Destroy()
end
local screenGui = Instance.new("ScreenGui")
screenGui.Name = uiName
screenGui.Parent = CoreGui
screenGui.DisplayOrder = -1
screenGui.Enabled = false
local label = Instance.new("TextLabel")
label.Font = Enum.Font.SourceSans
label.TextSize = 20
label.Text = "Rojo polling..."
label.BackgroundColor3 = Color3.fromRGB(31, 31, 31)
label.BackgroundTransparency = 0.5
label.BorderSizePixel = 0
label.TextColor3 = Color3.new(1, 1, 1)
label.Size = UDim2.new(0, 120, 0, 28)
label.Position = UDim2.new(0, 0, 0, 0)
label.Parent = screenGui
self._label = screenGui
-- If our UI was destroyed, we assume it was from another instance of
-- the Rojo plugin coming online.
--
-- Roblox doesn't notify plugins when they get unloaded, so this is the
-- best trigger we have right now unless we create a dedicated event
-- object.
screenGui.AncestryChanged:Connect(function(_, parent)
if parent == nil then
warn(MESSAGE_PLUGIN_CHANGED)
self:restart()
end
end)
end
return self
end
--[[
Clears all state and issues a notice to the user that the plugin has
restarted.
]]
function Plugin:restart()
self:stopPolling()
self._reconciler:destruct()
self._reconciler = Reconciler.new()
self._api = nil
self._polling = false
self._syncInProgress = false
end
function Plugin:getApi()
if self._api == nil then
return Api.connect(self._http)
:andThen(function(api)
self._api = api
return api
end, function(err)
return Promise.reject(err)
end)
end
return Promise.resolve(self._api)
end
function Plugin:connect()
print("Rojo: Testing connection...")
return self:getApi()
:andThen(function(api)
local ok, info = api:getInfo():await()
if not ok then
return Promise.reject(info)
end
print("Rojo: Server found!")
print("Rojo: Protocol version:", info.protocolVersion)
print("Rojo: Server version:", info.serverVersion)
end)
:catch(function(err)
if err == Api.Error.ServerIdMismatch then
warn(MESSAGE_SERVER_CHANGED)
self:restart()
return self:connect()
else
return Promise.reject(err)
end
end)
end
function Plugin:togglePolling()
if self._polling then
return self:stopPolling()
else
return self:startPolling()
end
end
function Plugin:stopPolling()
if not self._polling then
return Promise.resolve(false)
end
print("Rojo: Stopped polling server for changes.")
self._polling = false
self._label.Enabled = false
return Promise.resolve(true)
end
function Plugin:_pull(api, project, fileRoutes)
return api:read(fileRoutes)
:andThen(function(items)
for index = 1, #fileRoutes do
local fileRoute = fileRoutes[index]
local partitionName = fileRoute[1]
local partition = project.partitions[partitionName]
local item = items[index]
local partitionTargetRbxRoute = collectMatch(partition.target, "[^.]+")
-- If the item route's length was 1, we need to rename the instance to
-- line up with the partition's root object name.
if item ~= nil and #fileRoute == 1 then
local objectName = partition.target:match("[^.]+$")
item.Name = objectName
end
local itemRbxRoute = {}
for _, piece in ipairs(partitionTargetRbxRoute) do
table.insert(itemRbxRoute, piece)
end
for i = 2, #fileRoute do
table.insert(itemRbxRoute, fileRoute[i])
end
self._reconciler:reconcileRoute(itemRbxRoute, item, fileRoute)
end
end)
end
function Plugin:startPolling()
if self._polling then
return
end
print("Rojo: Starting to poll server for changes...")
self._polling = true
self._label.Enabled = true
return self:getApi()
:andThen(function(api)
local infoOk, info = api:getInfo():await()
if not infoOk then
return Promise.reject(info)
end
local syncOk, result = self:syncIn():await()
if not syncOk then
return Promise.reject(result)
end
while self._polling do
local changesOk, changes = api:getChanges():await()
if not changesOk then
return Promise.reject(changes)
end
if #changes > 0 then
local routes = {}
for _, change in ipairs(changes) do
table.insert(routes, change.route)
end
local pullOk, pullResult = self:_pull(api, info.project, routes):await()
if not pullOk then
return Promise.reject(pullResult)
end
end
wait(Config.pollingRate)
end
end)
:catch(function(err)
if err == Api.Error.ServerIdMismatch then
warn(MESSAGE_SERVER_CHANGED)
self:restart()
return self:startPolling()
else
self:stopPolling()
return Promise.reject(err)
end
end)
end
function Plugin:syncIn()
if self._syncInProgress then
warn("Rojo: Can't sync right now, because a sync is already in progress.")
return Promise.resolve()
end
self._syncInProgress = true
print("Rojo: Syncing from server...")
return self:getApi()
:andThen(function(api)
local ok, info = api:getInfo():await()
if not ok then
return Promise.reject(info)
end
local fileRoutes = {}
for name in pairs(info.project.partitions) do
table.insert(fileRoutes, {name})
end
local pullSuccess, pullResult = self:_pull(api, info.project, fileRoutes):await()
self._syncInProgress = false
if not pullSuccess then
return Promise.reject(pullResult)
end
print("Rojo: Sync successful!")
end)
:catch(function(err)
self._syncInProgress = false
if err == Api.Error.ServerIdMismatch then
warn(MESSAGE_SERVER_CHANGED)
self:restart()
return self:syncIn()
else
return Promise.reject(err)
end
end)
end
return Plugin

View File

@@ -1,281 +1,250 @@
local Logging = require(script.Parent.Logging)
local RouteMap = require(script.Parent.RouteMap)
local function makeInstanceMap()
local self = {
fromIds = {},
fromInstances = {},
}
local function classEqual(a, b)
assert(typeof(a) == "string")
assert(typeof(b) == "string")
function self:insert(id, instance)
self.fromIds[id] = instance
self.fromInstances[instance] = id
if a == "*" or b == "*" then
return true
end
function self:removeId(id)
local instance = self.fromIds[id]
if instance ~= nil then
self.fromIds[id] = nil
self.fromInstances[instance] = nil
else
Logging.warn("Attempted to remove nonexistant ID %s", tostring(id))
end
end
function self:removeInstance(instance)
local id = self.fromInstances[instance]
if id ~= nil then
self.fromInstances[instance] = nil
self.fromIds[id] = nil
else
Logging.warn("Attempted to remove nonexistant instance %s", tostring(instance))
end
end
function self:destroyId(id)
local instance = self.fromIds[id]
self:removeId(id)
if instance ~= nil then
local descendantsToDestroy = {}
for otherInstance in pairs(self.fromInstances) do
if otherInstance:IsDescendantOf(instance) then
table.insert(descendantsToDestroy, otherInstance)
end
end
for _, otherInstance in ipairs(descendantsToDestroy) do
self:removeInstance(otherInstance)
end
instance:Destroy()
else
Logging.warn("Attempted to destroy nonexistant ID %s", tostring(id))
end
end
return self
return a == b
end
local function setProperty(instance, key, value)
-- The 'Contents' property of LocalizationTable isn't directly exposed, but
-- has corresponding (deprecated) getters and setters.
if key == "Contents" and instance.ClassName == "LocalizationTable" then
instance:SetContents(value)
local function applyProperties(target, properties)
assert(typeof(target) == "Instance")
assert(typeof(properties) == "table")
for key, property in pairs(properties) do
-- TODO: Transform property value based on property.Type
-- Right now, we assume that 'value' is primitive!
target[key] = property.Value
end
end
--[[
Attempt to parent `rbx` to `parent`, doing nothing if:
* parent is already `parent`
* Changing parent threw an error
]]
local function reparent(rbx, parent)
assert(typeof(rbx) == "Instance")
assert(typeof(parent) == "Instance")
if rbx.Parent == parent then
return
end
-- If we don't have permissions to access this value at all, we can skip it.
local readSuccess, existingValue = pcall(function()
return instance[key]
-- Setting `Parent` can fail if:
-- * The object has been destroyed
-- * The object is a service and cannot be reparented
pcall(function()
rbx.Parent = parent
end)
end
if not readSuccess then
-- An error will be thrown if there was a permission issue or if the
-- property doesn't exist. In the latter case, we should tell the user
-- because it's probably their fault.
if existingValue:find("lacking permission") then
Logging.trace("Permission error reading property %s on class %s", tostring(key), instance.ClassName)
return
else
error(("Invalid property %s on class %s: %s"):format(tostring(key), instance.ClassName, existingValue), 2)
--[[
Attempts to match up Roblox instances and object specifiers for
reconciliation.
An object is considered a match if they have the same Name and ClassName.
primaryChildren and secondaryChildren can each be either a list of Roblox
instances or object specifiers. Since they share a common shape, switching
the two around isn't problematic!
visited is expected to be an empty table initially. It will be filled with
the set of children that have been visited so far.
]]
local function findNextChildPair(primaryChildren, secondaryChildren, visited)
for _, primaryChild in ipairs(primaryChildren) do
if not visited[primaryChild] then
visited[primaryChild] = true
for _, secondaryChild in ipairs(secondaryChildren) do
if classEqual(primaryChild.ClassName, secondaryChild.ClassName) and primaryChild.Name == secondaryChild.Name then
visited[secondaryChild] = true
return primaryChild, secondaryChild
end
end
return primaryChild, nil
end
end
local writeSuccess, err = pcall(function()
if existingValue ~= value then
instance[key] = value
end
end)
if not writeSuccess then
error(("Cannot set property %s on class %s: %s"):format(tostring(key), instance.ClassName, err), 2)
end
return true
return nil, nil
end
local Reconciler = {}
Reconciler.__index = Reconciler
function Reconciler.new()
local self = {
instanceMap = makeInstanceMap(),
local reconciler = {
_routeMap = RouteMap.new(),
}
return setmetatable(self, Reconciler)
setmetatable(reconciler, Reconciler)
return reconciler
end
function Reconciler:applyUpdate(requestedIds, virtualInstancesById)
-- This function may eventually be asynchronous; it will require calls to
-- the server to resolve instances that don't exist yet.
local visitedIds = {}
--[[
A semi-smart algorithm that attempts to apply the given item's children to
an existing Roblox object.
]]
function Reconciler:_reconcileChildren(rbx, item)
local visited = {}
local rbxChildren = rbx:GetChildren()
for _, id in ipairs(requestedIds) do
self:__applyUpdatePiece(id, visitedIds, virtualInstancesById)
-- Reconcile any children that were added or updated
while true do
local itemChild, rbxChild = findNextChildPair(item.Children, rbxChildren, visited)
if itemChild == nil then
break
end
local newRbxChild = self:reconcile(rbxChild, itemChild)
if newRbxChild ~= nil then
newRbxChild.Parent = rbx
end
end
-- Reconcile any children that were deleted
while true do
local rbxChild, itemChild = findNextChildPair(rbxChildren, item.Children, visited)
if rbxChild == nil then
break
end
local newRbxChild = self:reconcile(rbxChild, itemChild)
if newRbxChild ~= nil then
newRbxChild.Parent = rbx
end
end
end
--[[
Update an existing instance, including its properties and children, to match
the given information.
Construct a new Roblox object from the given item.
]]
function Reconciler:reconcile(virtualInstancesById, id, instance)
local virtualInstance = virtualInstancesById[id]
function Reconciler:_reify(item)
local className = item.ClassName
-- If an instance changes ClassName, we assume it's very different. That's
-- not always the case!
if virtualInstance.ClassName ~= instance.ClassName then
-- TODO: Preserve existing children instead?
local parent = instance.Parent
self.instanceMap:destroyId(id)
return self:__reify(virtualInstancesById, id, parent)
-- "*" represents a match of any class. It reifies as a folder!
if className == "*" then
className = "Folder"
end
self.instanceMap:insert(id, instance)
local rbx = Instance.new(className)
rbx.Name = item.Name
-- Some instances don't like being named, even if their name already matches
setProperty(instance, "Name", virtualInstance.Name)
applyProperties(rbx, item.Properties)
for key, value in pairs(virtualInstance.Properties) do
setProperty(instance, key, value.Value)
for _, child in ipairs(item.Children) do
reparent(self:_reify(child), rbx)
end
local existingChildren = instance:GetChildren()
local unvisitedExistingChildren = {}
for _, child in ipairs(existingChildren) do
unvisitedExistingChildren[child] = true
if item.Route ~= nil then
self._routeMap:insert(item.Route, rbx)
end
for _, childId in ipairs(virtualInstance.Children) do
local childData = virtualInstancesById[childId]
return rbx
end
local existingChildInstance
for instance in pairs(unvisitedExistingChildren) do
local ok, name, className = pcall(function()
return instance.Name, instance.ClassName
end)
--[[
Clears any state that the Reconciler has, stopping it completely.
]]
function Reconciler:destruct()
self._routeMap:destruct()
end
if ok then
if name == childData.Name and className == childData.ClassName then
existingChildInstance = instance
break
end
--[[
Apply the changes represented by the given item to a Roblox object that's a
child of the given instance.
]]
function Reconciler:reconcile(rbx, item)
-- Item was deleted
if item == nil then
if rbx ~= nil then
-- TODO: If this is a partition root, should we leave it alone?
self._routeMap:removeByRbx(rbx)
rbx:Destroy()
end
return nil
end
-- Item was created!
if rbx == nil then
return self:_reify(item)
end
-- Item changed type!
if not classEqual(rbx.ClassName, item.ClassName) then
self._routeMap:removeByRbx(rbx)
rbx:Destroy()
return self:_reify(item)
end
-- It's possible that the instance we're associating with this item hasn't
-- been inserted into the RouteMap yet.
if item.Route ~= nil then
self._routeMap:insert(item.Route, rbx)
end
applyProperties(rbx, item.Properties)
self:_reconcileChildren(rbx, item)
return rbx
end
function Reconciler:reconcileRoute(rbxRoute, item, fileRoute)
local parent
local rbx = game
for i = 1, #rbxRoute do
local piece = rbxRoute[i]
local child = rbx:FindFirstChild(piece)
-- We should get services instead of making folders here.
if rbx == game and child == nil then
local success
success, child = pcall(game.GetService, game, piece)
-- That isn't a valid service!
if not success then
child = nil
end
end
if existingChildInstance ~= nil then
unvisitedExistingChildren[existingChildInstance] = nil
self:reconcile(virtualInstancesById, childId, existingChildInstance)
else
self:__reify(virtualInstancesById, childId, instance)
-- We don't want to create a folder if we're reaching our target item!
if child == nil and i ~= #rbxRoute then
child = Instance.new("Folder")
child.Parent = rbx
child.Name = piece
end
parent = rbx
rbx = child
end
if self:__shouldClearUnknownInstances(virtualInstance) then
for existingChildInstance in pairs(unvisitedExistingChildren) do
self.instanceMap:removeInstance(existingChildInstance)
existingChildInstance:Destroy()
end
-- Let's check the route map!
if rbx == nil then
rbx = self._routeMap:get(fileRoute)
end
-- The root instance of a project won't have a parent, like the DataModel,
-- so we need to be careful here.
if virtualInstance.Parent ~= nil then
local parent = self.instanceMap.fromIds[virtualInstance.Parent]
rbx = self:reconcile(rbx, item)
if parent == nil then
Logging.info("Instance %s wanted parent of %s", tostring(id), tostring(virtualInstance.Parent))
error("Rojo bug: During reconciliation, an instance referred to an instance ID as parent that does not exist.")
end
-- Some instances, like services, don't like having their Parent
-- property poked, even if we're setting it to the same value.
setProperty(instance, "Parent", parent)
if instance.Parent ~= parent then
instance.Parent = parent
end
end
return instance
end
function Reconciler:__shouldClearUnknownInstances(virtualInstance)
if virtualInstance.Metadata ~= nil then
return not virtualInstance.Metadata.ignoreUnknownInstances
else
return true
if rbx ~= nil then
reparent(rbx, parent)
end
end
function Reconciler:__reify(virtualInstancesById, id, parent)
local virtualInstance = virtualInstancesById[id]
local instance = Instance.new(virtualInstance.ClassName)
for key, value in pairs(virtualInstance.Properties) do
-- TODO: Branch on value.Type
setProperty(instance, key, value.Value)
end
instance.Name = virtualInstance.Name
for _, childId in ipairs(virtualInstance.Children) do
self:__reify(virtualInstancesById, childId, instance)
end
setProperty(instance, "Parent", parent)
self.instanceMap:insert(id, instance)
return instance
end
function Reconciler:__applyUpdatePiece(id, visitedIds, virtualInstancesById)
if visitedIds[id] then
return
end
visitedIds[id] = true
local virtualInstance = virtualInstancesById[id]
local instance = self.instanceMap.fromIds[id]
-- The instance was deleted in this update
if virtualInstance == nil then
self.instanceMap:destroyId(id)
return
end
-- An instance we know about was updated
if instance ~= nil then
self:reconcile(virtualInstancesById, id, instance)
return instance
end
-- If the instance's parent already exists, we can stick it there
local parentInstance = self.instanceMap.fromIds[virtualInstance.Parent]
if parentInstance ~= nil then
self:__reify(virtualInstancesById, id, parentInstance)
return
end
-- Otherwise, we can check if this response payload contained the parent and
-- work from there instead.
local parentData = virtualInstancesById[virtualInstance.Parent]
if parentData ~= nil then
if visitedIds[virtualInstance.Parent] then
error("Rojo bug: An instance was present and marked as visited but its instance was missing")
end
self:__applyUpdatePiece(virtualInstance.Parent, visitedIds, virtualInstancesById)
return
end
Logging.trace("Instance ID %s, parent ID %s", tostring(id), tostring(virtualInstance.Parent))
error("Rojo NYI: Instances with parents that weren't mentioned in an update payload")
end
return Reconciler
return Reconciler

123
plugin/src/RouteMap.lua Normal file
View File

@@ -0,0 +1,123 @@
--[[
A map from Route objects (given by the server) to Roblox instances (created
by the plugin).
]]
local function hashRoute(route)
return table.concat(route, "/")
end
local RouteMap = {}
RouteMap.__index = RouteMap
function RouteMap.new()
local self = {
_map = {},
_reverseMap = {},
_connectionsByRbx = {},
}
setmetatable(self, RouteMap)
return self
end
function RouteMap:insert(route, rbx)
local hashed = hashRoute(route)
-- Make sure that each route and instance are only present in RouteMap once.
self:removeByRoute(route)
self:removeByRbx(rbx)
self._map[hashed] = rbx
self._reverseMap[rbx] = hashed
self._connectionsByRbx[rbx] = rbx.AncestryChanged:Connect(function(_, parent)
if parent == nil then
self:removeByRbx(rbx)
end
end)
end
function RouteMap:get(route)
return self._map[hashRoute(route)]
end
function RouteMap:removeByRoute(route)
local hashedRoute = hashRoute(route)
local rbx = self._map[hashedRoute]
if rbx ~= nil then
self:_removeInternal(rbx, hashedRoute)
end
end
function RouteMap:removeByRbx(rbx)
local hashedRoute = self._reverseMap[rbx]
if hashedRoute ~= nil then
self:_removeInternal(rbx, hashedRoute)
end
end
--[[
Correcly removes the given Roblox Instance/Route pair from the RouteMap.
]]
function RouteMap:_removeInternal(rbx, hashedRoute)
self._map[hashedRoute] = nil
self._reverseMap[rbx] = nil
self._connectionsByRbx[rbx]:Disconnect()
self._connectionsByRbx[rbx] = nil
self:_removeRbxDescendants(rbx)
end
--[[
Ensure that there are no descendants of the given Roblox Instance still
present in the map, guaranteeing that it has been cleaned out.
]]
function RouteMap:_removeRbxDescendants(parentRbx)
for rbx in pairs(self._reverseMap) do
if rbx:IsDescendantOf(parentRbx) then
self:removeByRbx(rbx)
end
end
end
--[[
Remove all items from the map and disconnect all connections, cleaning up
the RouteMap.
]]
function RouteMap:destruct()
self._map = {}
self._reverseMap = {}
for _, connection in pairs(self._connectionsByRbx) do
connection:Disconnect()
end
self._connectionsByRbx = {}
end
function RouteMap:visualize()
-- Log all of our keys so that the visualization has a stable order.
local keys = {}
for key in pairs(self._map) do
table.insert(keys, key)
end
table.sort(keys)
local buffer = {}
for _, key in ipairs(keys) do
local visualized = ("- %s: %s"):format(
key,
self._map[key]:GetFullName()
)
table.insert(buffer, visualized)
end
return table.concat(buffer, "\n")
end
return RouteMap

View File

@@ -1,97 +0,0 @@
local Rojo = script:FindFirstAncestor("Rojo")
local Promise = require(Rojo.Promise)
local ApiContext = require(script.Parent.ApiContext)
local Reconciler = require(script.Parent.Reconciler)
local Session = {}
Session.__index = Session
function Session.new(config)
local remoteUrl = ("http://%s:%s"):format(config.address, config.port)
local api = ApiContext.new(remoteUrl)
local self = {
onError = config.onError,
disconnected = false,
reconciler = Reconciler.new(),
api = api,
}
api:connect()
:andThen(function()
if self.disconnected then
return Promise.resolve()
end
return api:read({api.rootInstanceId})
:andThen(function(response)
if self.disconnected then
return Promise.resolve()
end
self.reconciler:reconcile(response.instances, api.rootInstanceId, game)
return self:__processMessages()
end)
end)
:catch(function(message)
self.disconnected = true
self.onError(message)
end)
return not self.disconnected, setmetatable(self, Session)
end
function Session:__processMessages()
if self.disconnected then
return Promise.resolve()
end
return self.api:retrieveMessages()
:andThen(function(messages)
local promise = Promise.resolve(nil)
for _, message in ipairs(messages) do
promise = promise:andThen(function()
return self:__onMessage(message)
end)
end
return promise
end)
:andThen(function()
return self:__processMessages()
end)
end
function Session:__onMessage(message)
if self.disconnected then
return Promise.resolve()
end
local requestedIds = {}
for _, id in ipairs(message.added) do
table.insert(requestedIds, id)
end
for _, id in ipairs(message.updated) do
table.insert(requestedIds, id)
end
for _, id in ipairs(message.removed) do
table.insert(requestedIds, id)
end
return self.api:read(requestedIds)
:andThen(function(response)
return self.reconciler:applyUpdate(requestedIds, response.instances)
end)
end
function Session:disconnect()
self.disconnected = true
end
return Session

View File

@@ -34,13 +34,7 @@ function Version.compare(a, b)
end
function Version.display(version)
local output = ("%d.%d.%d"):format(version[1], version[2], version[3])
if version[4] ~= nil then
output = output .. version[4]
end
return output
return table.concat(version, ".")
end
return Version
return Version

View File

@@ -1,19 +0,0 @@
if not plugin then
return
end
local Roact = require(script.Parent.Roact)
Roact.setGlobalConfig({
elementTracing = true,
})
local App = require(script.Components.App)
local app = Roact.createElement(App, {
plugin = plugin,
})
Roact.mount(app, game:GetService("CoreGui"), "Rojo UI")
-- TODO: Detect another instance of Rojo coming online and shut down this one.

View File

@@ -1,38 +0,0 @@
{
"name": "rojo",
"tree": {
"$className": "DataModel",
"ReplicatedStorage": {
"$className": "ReplicatedStorage",
"Rojo": {
"$className": "Folder",
"Plugin": {
"$path": "src"
},
"Roact": {
"$path": "modules/roact/lib"
},
"Rodux": {
"$path": "modules/rodux/lib"
},
"RoactRodux": {
"$path": "modules/roact-rodux/lib"
},
"Promise": {
"$path": "modules/promise/lib"
}
},
"TestEZ": {
"$path": "modules/testez/lib"
}
},
"TestService": {
"$className": "TestService",
"TestBootstrap": {
"$path": "testBootstrap.server.lua"
}
}
}
}

View File

@@ -1,2 +0,0 @@
local TestEZ = require(game.ReplicatedStorage.TestEZ)
TestEZ.TestBootstrap:run(game.ReplicatedStorage.Rojo.plugin)

View File

@@ -1,5 +0,0 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Session = require(ReplicatedStorage.Modules.Rojo.Session)
Session.new()

View File

@@ -0,0 +1,2 @@
local TestEZ = require(game.ReplicatedStorage.TestEZ)
TestEZ.TestBootstrap:run(game.ReplicatedStorage.Rojo.plugin)

View File

@@ -1,6 +0,0 @@
[package]
name = "rojo-e2e"
version = "0.1.0"
authors = ["Lucien Greathouse <me@lpghatguy.com>"]
[dependencies]

View File

@@ -1,2 +0,0 @@
# Rojo End-to-End
This is a WIP test runner designed for Rojo. It will eventually start up the Rojo server and plugin and test functionality end-to-end.

View File

@@ -1,32 +0,0 @@
use std::{
path::Path,
process::Command,
thread,
time::Duration,
};
fn main() {
let plugin_path = Path::new("../plugin");
let server_path = Path::new("../server");
let tests_path = Path::new("../tests");
let server = Command::new("cargo")
.args(&["run", "--", "serve", "../test-projects/empty"])
.current_dir(server_path)
.spawn();
thread::sleep(Duration::from_millis(1000));
// TODO: Wait for server to start responding on the right port
let test_client = Command::new("lua")
.args(&["runTest.lua", "tests/empty.lua"])
.current_dir(plugin_path)
.spawn();
thread::sleep(Duration::from_millis(300));
// TODO: Collect output from the client for success/failure?
println!("Dying!");
}

5
server/.editorconfig Normal file
View File

@@ -0,0 +1,5 @@
[*.rs]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true

2
server/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/target/
**/*.rs.bk

1104
server/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,45 +1,22 @@
[package]
name = "rojo"
version = "0.5.0-alpha.0"
version = "0.4.13"
authors = ["Lucien Greathouse <me@lpghatguy.com>"]
description = "A tool to create robust Roblox projects"
license = "MIT"
repository = "https://github.com/LPGhatguy/rojo"
edition = "2018"
[lib]
name = "librojo"
path = "src/lib.rs"
[[bin]]
name = "rojo"
path = "src/bin.rs"
[features]
default = []
bundle-plugin = []
[dependencies]
clap = "2.27"
csv = "1.0"
env_logger = "0.5"
failure = "0.1.3"
log = "0.4"
maplit = "1.0.1"
notify = "4.0"
rand = "0.4"
regex = "1.0"
reqwest = "0.9.5"
rouille = "2.1"
clap = "2.27.1"
rouille = "2.1.0"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
uuid = { version = "0.7", features = ["v4", "serde"] }
rbx_tree = "0.1.0"
rbx_xml = "0.1.0"
rbx_binary = "0.1.0"
[dev-dependencies]
tempfile = "3.0"
walkdir = "2.1"
lazy_static = "1.2"
notify = "4.0.0"
rand = "0.3"
regex = "0.2"
lazy_static = "1.0"

View File

@@ -1,4 +0,0 @@
# Rojo Server
This is the source to the Rojo server.
Documentation is WIP.

View File

@@ -1,30 +1,30 @@
#[macro_use] extern crate log;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate rouille;
#[macro_use] extern crate clap;
#[macro_use] extern crate lazy_static;
extern crate notify;
extern crate rand;
extern crate serde;
extern crate serde_json;
extern crate regex;
use std::{
path::{Path, PathBuf},
env,
process,
};
pub mod web;
pub mod core;
pub mod project;
pub mod pathext;
pub mod vfs;
pub mod rbx;
pub mod plugin;
pub mod plugins;
pub mod commands;
use clap::clap_app;
use std::path::{Path, PathBuf};
use std::process;
use librojo::commands;
fn make_path_absolute(value: &Path) -> PathBuf {
if value.is_absolute() {
PathBuf::from(value)
} else {
let current_dir = env::current_dir().unwrap();
current_dir.join(value)
}
}
use pathext::canonicalish;
fn main() {
env_logger::Builder::from_default_env()
.default_format_timestamp(false)
.init();
let mut app = clap_app!(Rojo =>
let matches = clap_app!(rojo =>
(version: env!("CARGO_PKG_VERSION"))
(author: env!("CARGO_PKG_AUTHORS"))
(about: env!("CARGO_PKG_DESCRIPTION"))
@@ -32,7 +32,6 @@ fn main() {
(@subcommand init =>
(about: "Creates a new Rojo project")
(@arg PATH: "Path to the place to create the project. Defaults to the current directory.")
(@arg kind: --kind +takes_value "The kind of project to create, 'place' or 'model'. Defaults to place.")
)
(@subcommand serve =>
@@ -41,132 +40,58 @@ fn main() {
(@arg port: --port +takes_value "The port to listen on. Defaults to 8000.")
)
(@subcommand build =>
(about: "Generates an rbxmx model file from the project.")
(@arg PROJECT: "Path to the project to serve. Defaults to the current directory.")
(@arg output: --output -o +takes_value +required "Where to output the result.")
(@subcommand pack =>
(about: "Packs the project into a GUI installer bundle. NOT YET IMPLEMENTED!")
(@arg PROJECT: "Path to the project to pack. Defaults to the current directory.")
)
(@subcommand upload =>
(about: "Generates a place or model file out of the project and uploads it to Roblox.")
(@arg PROJECT: "Path to the project to upload. Defaults to the current directory.")
(@arg kind: --kind +takes_value "The kind of asset to generate, 'place', or 'model'. Defaults to place.")
(@arg cookie: --cookie +takes_value +required "Security cookie to authenticate with.")
(@arg asset_id: --asset_id +takes_value +required "Asset ID to upload to.")
)
);
(@arg verbose: --verbose "Enable extended logging.")
).get_matches();
// `get_matches` consumes self for some reason.
let matches = app.clone().get_matches();
let verbose = match matches.occurrences_of("verbose") {
0 => false,
_ => true,
};
match matches.subcommand() {
("init", Some(sub_matches)) => {
let fuzzy_project_path = make_path_absolute(Path::new(sub_matches.value_of("PATH").unwrap_or("")));
let kind = sub_matches.value_of("kind");
("init", sub_matches) => {
let sub_matches = sub_matches.unwrap();
let project_path = Path::new(sub_matches.value_of("PATH").unwrap_or("."));
let full_path = canonicalish(project_path);
let options = commands::InitOptions {
fuzzy_project_path,
kind,
};
match commands::init(&options) {
Ok(_) => {},
Err(e) => {
error!("{}", e);
process::exit(1);
},
}
commands::init(&full_path);
},
("serve", Some(sub_matches)) => {
let fuzzy_project_path = match sub_matches.value_of("PROJECT") {
Some(v) => make_path_absolute(Path::new(v)),
("serve", sub_matches) => {
let sub_matches = sub_matches.unwrap();
let project_path = match sub_matches.value_of("PROJECT") {
Some(v) => canonicalish(PathBuf::from(v)),
None => std::env::current_dir().unwrap(),
};
let port = match sub_matches.value_of("port") {
Some(v) => match v.parse::<u16>() {
Ok(port) => Some(port),
Err(_) => {
error!("Invalid port {}", v);
process::exit(1);
},
},
None => None,
};
let options = commands::ServeOptions {
fuzzy_project_path,
port,
};
match commands::serve(&options) {
Ok(_) => {},
Err(e) => {
error!("{}", e);
process::exit(1);
},
}
},
("build", Some(sub_matches)) => {
let fuzzy_project_path = match sub_matches.value_of("PROJECT") {
Some(v) => make_path_absolute(Path::new(v)),
None => std::env::current_dir().unwrap(),
};
let output_file = make_path_absolute(Path::new(sub_matches.value_of("output").unwrap()));
let options = commands::BuildOptions {
fuzzy_project_path,
output_file,
output_kind: None, // TODO: Accept from argument
};
match commands::build(&options) {
Ok(_) => {},
Err(e) => {
error!("{}", e);
process::exit(1);
},
}
},
("upload", Some(sub_matches)) => {
let fuzzy_project_path = match sub_matches.value_of("PROJECT") {
Some(v) => make_path_absolute(Path::new(v)),
None => std::env::current_dir().unwrap(),
};
let kind = sub_matches.value_of("kind");
let security_cookie = sub_matches.value_of("cookie").unwrap();
let asset_id: u64 = {
let arg = sub_matches.value_of("asset_id").unwrap();
match arg.parse() {
Ok(v) => v,
Err(_) => {
error!("Invalid place ID {}", arg);
process::exit(1);
let port = {
match sub_matches.value_of("port") {
Some(source) => match source.parse::<u64>() {
Ok(value) => Some(value),
Err(_) => {
eprintln!("Invalid port '{}'", source);
process::exit(1);
},
},
None => None,
}
};
let options = commands::UploadOptions {
fuzzy_project_path,
security_cookie: security_cookie.to_string(),
asset_id,
kind,
};
match commands::upload(&options) {
Ok(_) => {},
Err(e) => {
error!("{}", e);
process::exit(1);
},
}
commands::serve(&project_path, verbose, port);
},
("pack", _) => {
eprintln!("'rojo pack' is not yet implemented!");
process::exit(1);
},
_ => {
app.print_help().expect("Could not print help text to stdout!");
eprintln!("Please specify a subcommand!");
eprintln!("Try 'rojo help' for information.");
process::exit(1);
},
}
}
}

View File

@@ -1,131 +0,0 @@
use std::{
path::PathBuf,
fs::File,
io,
};
use failure::Fail;
use crate::{
rbx_session::construct_oneoff_tree,
project::{Project, ProjectLoadFuzzyError},
imfs::Imfs,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputKind {
Rbxmx,
Rbxlx,
Rbxm,
Rbxl,
}
fn detect_output_kind(options: &BuildOptions) -> Option<OutputKind> {
let extension = options.output_file.extension()?.to_str()?;
match extension {
"rbxlx" => Some(OutputKind::Rbxlx),
"rbxmx" => Some(OutputKind::Rbxmx),
"rbxl" => Some(OutputKind::Rbxl),
"rbxm" => Some(OutputKind::Rbxm),
_ => None,
}
}
#[derive(Debug)]
pub struct BuildOptions {
pub fuzzy_project_path: PathBuf,
pub output_file: PathBuf,
pub output_kind: Option<OutputKind>,
}
#[derive(Debug, Fail)]
pub enum BuildError {
#[fail(display = "Could not detect what kind of file to create")]
UnknownOutputKind,
#[fail(display = "Project load error: {}", _0)]
ProjectLoadError(#[fail(cause)] ProjectLoadFuzzyError),
#[fail(display = "IO error: {}", _0)]
IoError(#[fail(cause)] io::Error),
#[fail(display = "XML model file error")]
XmlModelEncodeError(rbx_xml::EncodeError),
#[fail(display = "Binary model file error")]
BinaryModelEncodeError(rbx_binary::EncodeError)
}
impl From<ProjectLoadFuzzyError> for BuildError {
fn from(error: ProjectLoadFuzzyError) -> BuildError {
BuildError::ProjectLoadError(error)
}
}
impl From<io::Error> for BuildError {
fn from(error: io::Error) -> BuildError {
BuildError::IoError(error)
}
}
impl From<rbx_xml::EncodeError> for BuildError {
fn from(error: rbx_xml::EncodeError) -> BuildError {
BuildError::XmlModelEncodeError(error)
}
}
impl From<rbx_binary::EncodeError> for BuildError {
fn from(error: rbx_binary::EncodeError) -> BuildError {
BuildError::BinaryModelEncodeError(error)
}
}
pub fn build(options: &BuildOptions) -> Result<(), BuildError> {
let output_kind = options.output_kind
.or_else(|| detect_output_kind(options))
.ok_or(BuildError::UnknownOutputKind)?;
info!("Hoping to generate file of type {:?}", output_kind);
info!("Looking for project at {}", options.fuzzy_project_path.display());
let project = Project::load_fuzzy(&options.fuzzy_project_path)?;
info!("Found project at {}", project.file_location.display());
info!("Using project {:#?}", project);
let mut imfs = Imfs::new();
imfs.add_roots_from_project(&project)?;
let tree = construct_oneoff_tree(&project, &imfs);
let mut file = File::create(&options.output_file)?;
match output_kind {
OutputKind::Rbxmx => {
// Model files include the root instance of the tree and all its
// descendants.
let root_id = tree.get_root_id();
rbx_xml::encode(&tree, &[root_id], &mut file)?;
},
OutputKind::Rbxlx => {
// Place files don't contain an entry for the DataModel, but our
// RbxTree representation does.
let root_id = tree.get_root_id();
let top_level_ids = tree.get_instance(root_id).unwrap().get_children_ids();
rbx_xml::encode(&tree, top_level_ids, &mut file)?;
},
OutputKind::Rbxm => {
let root_id = tree.get_root_id();
rbx_binary::encode(&tree, &[root_id], &mut file)?;
},
OutputKind::Rbxl => {
let root_id = tree.get_root_id();
let top_level_ids = tree.get_instance(root_id).unwrap().get_children_ids();
rbx_binary::encode(&tree, top_level_ids, &mut file)?;
},
}
Ok(())
}

View File

@@ -1,46 +1,16 @@
use std::{
path::PathBuf,
};
use std::path::PathBuf;
use std::process;
use failure::Fail;
use project::Project;
use crate::project::{Project, ProjectInitError};
#[derive(Debug, Fail)]
pub enum InitError {
#[fail(display = "Invalid project kind '{}', valid kinds are 'place' and 'model'", _0)]
InvalidKind(String),
#[fail(display = "Project init error: {}", _0)]
ProjectInitError(#[fail(cause)] ProjectInitError)
}
impl From<ProjectInitError> for InitError {
fn from(error: ProjectInitError) -> InitError {
InitError::ProjectInitError(error)
pub fn init(project_path: &PathBuf) {
match Project::init(project_path) {
Ok(_) => {
println!("Created new empty project at {}", project_path.display());
},
Err(e) => {
eprintln!("Failed to create new project.\n{}", e);
process::exit(1);
},
}
}
#[derive(Debug)]
pub struct InitOptions<'a> {
pub fuzzy_project_path: PathBuf,
pub kind: Option<&'a str>,
}
pub fn init(options: &InitOptions) -> Result<(), InitError> {
let (project_path, project_kind) = match options.kind {
Some("place") | None => {
let path = Project::init_place(&options.fuzzy_project_path)?;
(path, "place")
},
Some("model") => {
let path = Project::init_model(&options.fuzzy_project_path)?;
(path, "model")
},
Some(invalid) => return Err(InitError::InvalidKind(invalid.to_string())),
};
println!("Created new {} project file at {}", project_kind, project_path.display());
Ok(())
}

View File

@@ -1,9 +1,5 @@
mod serve;
mod init;
mod build;
mod upload;
pub use self::serve::*;
pub use self::init::*;
pub use self::build::*;
pub use self::upload::*;

View File

@@ -1,54 +1,98 @@
use std::{
path::PathBuf,
sync::Arc,
};
use std::path::{Path, PathBuf};
use std::process;
use std::sync::{Arc, Mutex};
use std::thread;
use failure::Fail;
use rand;
use crate::{
project::{Project, ProjectLoadFuzzyError},
web::Server,
session::Session,
};
use project::{Project, ProjectLoadError};
use plugin::{PluginChain};
use plugins::{DefaultPlugin, JsonModelPlugin, ScriptPlugin};
use vfs::{VfsSession, VfsWatcher};
use web;
const DEFAULT_PORT: u16 = 34872;
pub fn serve(project_path: &PathBuf, verbose: bool, port: Option<u64>) {
let server_id = rand::random::<u64>();
#[derive(Debug)]
pub struct ServeOptions {
pub fuzzy_project_path: PathBuf,
pub port: Option<u16>,
}
let project = match Project::load(project_path) {
Ok(project) => {
println!("Using project \"{}\" from {}", project.name, project_path.display());
project
},
Err(err) => {
match err {
ProjectLoadError::InvalidJson(serde_err) => {
eprintln!("Project contained invalid JSON!");
eprintln!("{}", project_path.display());
eprintln!("Error: {}", serde_err);
#[derive(Debug, Fail)]
pub enum ServeError {
#[fail(display = "Project load error: {}", _0)]
ProjectLoadError(#[fail(cause)] ProjectLoadFuzzyError),
}
process::exit(1);
},
ProjectLoadError::FailedToOpen | ProjectLoadError::FailedToRead => {
eprintln!("Found project file, but failed to read it!");
eprintln!("Check the permissions of the project file at {}", project_path.display());
impl From<ProjectLoadFuzzyError> for ServeError {
fn from(error: ProjectLoadFuzzyError) -> ServeError {
ServeError::ProjectLoadError(error)
process::exit(1);
},
ProjectLoadError::DidNotExist => {
eprintln!("Found no project file! Create one using 'rojo init'");
eprintln!("Checked for a project at {}", project_path.display());
process::exit(1);
},
}
},
};
if project.partitions.len() == 0 {
println!("");
println!("This project has no partitions and will not do anything when served!");
println!("This is usually a mistake -- edit rojo.json!");
println!("");
}
lazy_static! {
static ref PLUGIN_CHAIN: PluginChain = PluginChain::new(vec![
Box::new(ScriptPlugin::new()),
Box::new(JsonModelPlugin::new()),
Box::new(DefaultPlugin::new()),
]);
}
let vfs = {
let mut vfs = VfsSession::new(&PLUGIN_CHAIN);
for (name, project_partition) in &project.partitions {
let path = {
let given_path = Path::new(&project_partition.path);
if given_path.is_absolute() {
given_path.to_path_buf()
} else {
project_path.join(given_path)
}
};
vfs.insert_partition(name, path);
}
Arc::new(Mutex::new(vfs))
};
{
let vfs = vfs.clone();
thread::spawn(move || {
VfsWatcher::new(vfs).start();
});
}
let web_config = web::WebConfig {
verbose,
port: port.unwrap_or(project.serve_port),
server_id,
};
println!("Server listening on port {}", web_config.port);
web::start(web_config, project.clone(), &PLUGIN_CHAIN, vfs.clone());
}
pub fn serve(options: &ServeOptions) -> Result<(), ServeError> {
info!("Looking for project at {}", options.fuzzy_project_path.display());
let project = Arc::new(Project::load_fuzzy(&options.fuzzy_project_path)?);
info!("Found project at {}", project.file_location.display());
info!("Using project {:#?}", project);
let session = Arc::new(Session::new(Arc::clone(&project)).unwrap());
let server = Server::new(Arc::clone(&session));
let port = options.port
.or(project.serve_port)
.unwrap_or(DEFAULT_PORT);
println!("Rojo server listening on port {}", port);
server.listen(port);
Ok(())
}

View File

@@ -1,114 +0,0 @@
use std::{
path::PathBuf,
io,
};
use failure::Fail;
use reqwest::header::{ACCEPT, USER_AGENT, CONTENT_TYPE, COOKIE};
use crate::{
rbx_session::construct_oneoff_tree,
project::{Project, ProjectLoadFuzzyError},
imfs::Imfs,
};
#[derive(Debug, Fail)]
pub enum UploadError {
#[fail(display = "Roblox API Error: {}", _0)]
RobloxApiError(String),
#[fail(display = "Invalid asset kind: {}", _0)]
InvalidKind(String),
#[fail(display = "Project load error: {}", _0)]
ProjectLoadError(#[fail(cause)] ProjectLoadFuzzyError),
#[fail(display = "IO error: {}", _0)]
IoError(#[fail(cause)] io::Error),
#[fail(display = "HTTP error: {}", _0)]
HttpError(#[fail(cause)] reqwest::Error),
#[fail(display = "XML model file error")]
XmlModelEncodeError(rbx_xml::EncodeError),
}
impl From<ProjectLoadFuzzyError> for UploadError {
fn from(error: ProjectLoadFuzzyError) -> UploadError {
UploadError::ProjectLoadError(error)
}
}
impl From<io::Error> for UploadError {
fn from(error: io::Error) -> UploadError {
UploadError::IoError(error)
}
}
impl From<reqwest::Error> for UploadError {
fn from(error: reqwest::Error) -> UploadError {
UploadError::HttpError(error)
}
}
impl From<rbx_xml::EncodeError> for UploadError {
fn from(error: rbx_xml::EncodeError) -> UploadError {
UploadError::XmlModelEncodeError(error)
}
}
#[derive(Debug)]
pub struct UploadOptions<'a> {
pub fuzzy_project_path: PathBuf,
pub security_cookie: String,
pub asset_id: u64,
pub kind: Option<&'a str>,
}
pub fn upload(options: &UploadOptions) -> Result<(), UploadError> {
// TODO: Switch to uploading binary format?
info!("Looking for project at {}", options.fuzzy_project_path.display());
let project = Project::load_fuzzy(&options.fuzzy_project_path)?;
info!("Found project at {}", project.file_location.display());
info!("Using project {:#?}", project);
let mut imfs = Imfs::new();
imfs.add_roots_from_project(&project)?;
let tree = construct_oneoff_tree(&project, &imfs);
let root_id = tree.get_root_id();
let mut contents = Vec::new();
match options.kind {
Some("place") | None => {
let top_level_ids = tree.get_instance(root_id).unwrap().get_children_ids();
rbx_xml::encode(&tree, top_level_ids, &mut contents)?;
},
Some("model") => {
rbx_xml::encode(&tree, &[root_id], &mut contents)?;
},
Some(invalid) => return Err(UploadError::InvalidKind(invalid.to_owned())),
}
let url = format!("https://data.roblox.com/Data/Upload.ashx?assetid={}", options.asset_id);
let client = reqwest::Client::new();
let mut response = client.post(&url)
.header(COOKIE, format!(".ROBLOSECURITY={}", &options.security_cookie))
.header(USER_AGENT, "Roblox/WinInet")
.header("Requester", "Client")
.header(CONTENT_TYPE, "application/xml")
.header(ACCEPT, "application/json")
.body(contents)
.send()?;
if !response.status().is_success() {
return Err(UploadError::RobloxApiError(response.text()?));
}
Ok(())
}

1
server/src/core.rs Normal file
View File

@@ -0,0 +1 @@
pub type Route = Vec<String>;

View File

@@ -1,115 +0,0 @@
use std::{
sync::{mpsc, Arc, Mutex},
time::Duration,
thread,
};
use notify::{
self,
DebouncedEvent,
RecommendedWatcher,
RecursiveMode,
Watcher,
};
use crate::{
imfs::Imfs,
rbx_session::RbxSession,
};
const WATCH_TIMEOUT: Duration = Duration::from_millis(100);
fn handle_event(imfs: &Mutex<Imfs>, rbx_session: &Mutex<RbxSession>, event: DebouncedEvent) {
match event {
DebouncedEvent::Create(path) => {
{
let mut imfs = imfs.lock().unwrap();
imfs.path_created(&path).unwrap();
}
{
let mut rbx_session = rbx_session.lock().unwrap();
rbx_session.path_created(&path);
}
},
DebouncedEvent::Write(path) => {
{
let mut imfs = imfs.lock().unwrap();
imfs.path_updated(&path).unwrap();
}
{
let mut rbx_session = rbx_session.lock().unwrap();
rbx_session.path_updated(&path);
}
},
DebouncedEvent::Remove(path) => {
{
let mut imfs = imfs.lock().unwrap();
imfs.path_removed(&path).unwrap();
}
{
let mut rbx_session = rbx_session.lock().unwrap();
rbx_session.path_removed(&path);
}
},
DebouncedEvent::Rename(from_path, to_path) => {
{
let mut imfs = imfs.lock().unwrap();
imfs.path_moved(&from_path, &to_path).unwrap();
}
{
let mut rbx_session = rbx_session.lock().unwrap();
rbx_session.path_renamed(&from_path, &to_path);
}
},
_ => {},
}
}
/// Watches for changes on the filesystem and links together the in-memory
/// filesystem and in-memory Roblox tree.
pub struct FsWatcher {
#[allow(unused)]
watchers: Vec<RecommendedWatcher>,
}
impl FsWatcher {
pub fn start(imfs: Arc<Mutex<Imfs>>, rbx_session: Arc<Mutex<RbxSession>>) -> FsWatcher {
let mut watchers = Vec::new();
{
let imfs_temp = imfs.lock().unwrap();
for root_path in imfs_temp.get_roots() {
let (watch_tx, watch_rx) = mpsc::channel();
let mut watcher = notify::watcher(watch_tx, WATCH_TIMEOUT)
.expect("Could not create `notify` watcher");
watcher.watch(root_path, RecursiveMode::Recursive)
.expect("Could not watch directory");
watchers.push(watcher);
let imfs = Arc::clone(&imfs);
let rbx_session = Arc::clone(&rbx_session);
let root_path = root_path.clone();
thread::spawn(move || {
info!("Watcher thread ({}) started", root_path.display());
while let Ok(event) = watch_rx.recv() {
handle_event(&imfs, &rbx_session, event);
}
info!("Watcher thread ({}) stopped", root_path.display());
});
}
}
FsWatcher {
watchers,
}
}
}

View File

@@ -1,223 +0,0 @@
use std::{
collections::{HashMap, HashSet},
path::{Path, PathBuf},
fs,
io,
};
use crate::project::{Project, ProjectNode};
fn add_sync_points(imfs: &mut Imfs, project_node: &ProjectNode) -> io::Result<()> {
match project_node {
ProjectNode::Instance(node) => {
for child in node.children.values() {
add_sync_points(imfs, child)?;
}
},
ProjectNode::SyncPoint(node) => {
imfs.add_root(&node.path)?;
},
}
Ok(())
}
/// The in-memory filesystem keeps a mirror of all files being watcher by Rojo
/// in order to deduplicate file changes in the case of bidirectional syncing
/// from Roblox Studio.
#[derive(Debug, Clone)]
pub struct Imfs {
items: HashMap<PathBuf, ImfsItem>,
roots: HashSet<PathBuf>,
}
impl Imfs {
pub fn new() -> Imfs {
Imfs {
items: HashMap::new(),
roots: HashSet::new(),
}
}
pub fn add_roots_from_project(&mut self, project: &Project) -> io::Result<()> {
add_sync_points(self, &project.tree)
}
pub fn get_roots(&self) -> &HashSet<PathBuf> {
&self.roots
}
pub fn get_items(&self) -> &HashMap<PathBuf, ImfsItem> {
&self.items
}
pub fn get(&self, path: &Path) -> Option<&ImfsItem> {
debug_assert!(path.is_absolute());
debug_assert!(self.is_within_roots(path));
self.items.get(path)
}
pub fn add_root(&mut self, path: &Path) -> io::Result<()> {
debug_assert!(path.is_absolute());
debug_assert!(!self.is_within_roots(path));
self.roots.insert(path.to_path_buf());
self.read_from_disk(path)
}
pub fn path_created(&mut self, path: &Path) -> io::Result<()> {
debug_assert!(path.is_absolute());
debug_assert!(self.is_within_roots(path));
self.read_from_disk(path)
}
pub fn path_updated(&mut self, path: &Path) -> io::Result<()> {
debug_assert!(path.is_absolute());
debug_assert!(self.is_within_roots(path));
self.read_from_disk(path)
}
pub fn path_removed(&mut self, path: &Path) -> io::Result<()> {
debug_assert!(path.is_absolute());
debug_assert!(self.is_within_roots(path));
self.remove_item(path);
if let Some(parent_path) = path.parent() {
self.unlink_child(parent_path, path);
}
Ok(())
}
pub fn path_moved(&mut self, from_path: &Path, to_path: &Path) -> io::Result<()> {
debug_assert!(from_path.is_absolute());
debug_assert!(self.is_within_roots(from_path));
debug_assert!(to_path.is_absolute());
debug_assert!(self.is_within_roots(to_path));
self.path_removed(from_path)?;
self.path_created(to_path)?;
Ok(())
}
pub fn get_root_for_path<'a>(&'a self, path: &Path) -> Option<&'a Path> {
for root_path in &self.roots {
if path.starts_with(root_path) {
return Some(root_path);
}
}
None
}
fn remove_item(&mut self, path: &Path) {
if let Some(ImfsItem::Directory(directory)) = self.items.remove(path) {
for child_path in &directory.children {
self.remove_item(child_path);
}
}
}
fn unlink_child(&mut self, parent: &Path, child: &Path) {
let parent_item = self.items.get_mut(parent);
match parent_item {
Some(ImfsItem::Directory(directory)) => {
directory.children.remove(child);
},
_ => {
panic!("Tried to unlink child of path that wasn't a directory!");
},
}
}
fn link_child(&mut self, parent: &Path, child: &Path) {
if self.is_within_roots(parent) {
let parent_item = self.items.get_mut(parent);
match parent_item {
Some(ImfsItem::Directory(directory)) => {
directory.children.insert(child.to_path_buf());
},
_ => {
panic!("Tried to link child of path that wasn't a directory!");
},
}
}
}
fn read_from_disk(&mut self, path: &Path) -> io::Result<()> {
let metadata = fs::metadata(path)?;
if metadata.is_file() {
let contents = fs::read(path)?;
let item = ImfsItem::File(ImfsFile {
path: path.to_path_buf(),
contents,
});
self.items.insert(path.to_path_buf(), item);
if let Some(parent_path) = path.parent() {
self.link_child(parent_path, path);
}
Ok(())
} else if metadata.is_dir() {
let item = ImfsItem::Directory(ImfsDirectory {
path: path.to_path_buf(),
children: HashSet::new(),
});
self.items.insert(path.to_path_buf(), item);
for entry in fs::read_dir(path)? {
let entry = entry?;
let child_path = entry.path();
self.read_from_disk(&child_path)?;
}
if let Some(parent_path) = path.parent() {
self.link_child(parent_path, path);
}
Ok(())
} else {
panic!("Unexpected non-file, non-directory item");
}
}
fn is_within_roots(&self, path: &Path) -> bool {
for root_path in &self.roots {
if path.starts_with(root_path) {
return true;
}
}
false
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImfsFile {
pub path: PathBuf,
pub contents: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImfsDirectory {
pub path: PathBuf,
pub children: HashSet<PathBuf>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ImfsItem {
File(ImfsFile),
Directory(ImfsDirectory),
}

View File

@@ -1,23 +0,0 @@
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
#[cfg(test)]
extern crate tempfile;
// pub mod roblox_studio;
pub mod commands;
pub mod fs_watcher;
pub mod imfs;
pub mod message_queue;
pub mod path_map;
pub mod project;
pub mod rbx_session;
pub mod rbx_snapshot;
pub mod session;
pub mod session_id;
pub mod visualize;
pub mod web;
pub mod web_util;

View File

@@ -1,79 +0,0 @@
use std::{
collections::HashMap,
sync::{
mpsc,
atomic::{AtomicUsize, Ordering},
RwLock,
Mutex,
},
};
/// A unique identifier, not guaranteed to be generated in any order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ListenerId(usize);
/// Generate a new ID, which has no defined ordering.
pub fn get_listener_id() -> ListenerId {
static LAST_ID: AtomicUsize = AtomicUsize::new(0);
ListenerId(LAST_ID.fetch_add(1, Ordering::SeqCst))
}
#[derive(Default)]
pub struct MessageQueue<T> {
messages: RwLock<Vec<T>>,
message_listeners: Mutex<HashMap<ListenerId, mpsc::Sender<()>>>,
}
impl<T: Clone> MessageQueue<T> {
pub fn new() -> MessageQueue<T> {
MessageQueue {
messages: RwLock::new(Vec::new()),
message_listeners: Mutex::new(HashMap::new()),
}
}
pub fn push_messages(&self, new_messages: &[T]) {
let message_listeners = self.message_listeners.lock().unwrap();
{
let mut messages = self.messages.write().unwrap();
messages.extend_from_slice(new_messages);
}
for listener in message_listeners.values() {
listener.send(()).unwrap();
}
}
pub fn subscribe(&self, sender: mpsc::Sender<()>) -> ListenerId {
let id = get_listener_id();
let mut message_listeners = self.message_listeners.lock().unwrap();
message_listeners.insert(id, sender);
id
}
pub fn unsubscribe(&self, id: ListenerId) {
let mut message_listeners = self.message_listeners.lock().unwrap();
message_listeners.remove(&id);
}
pub fn get_message_cursor(&self) -> u32 {
self.messages.read().unwrap().len() as u32
}
pub fn get_messages_since(&self, cursor: u32) -> (u32, Vec<T>) {
let messages = self.messages.read().unwrap();
let current_cursor = messages.len() as u32;
// Cursor is out of bounds or there are no new messages
if cursor >= current_cursor {
return (current_cursor, Vec::new());
}
(current_cursor, messages[(cursor as usize)..].to_vec())
}
}

View File

@@ -1,96 +0,0 @@
use std::{
path::{self, Path, PathBuf},
collections::{HashMap, HashSet},
};
#[derive(Debug, Serialize)]
struct PathMapNode<T> {
value: T,
children: HashSet<PathBuf>,
}
/// A map from paths to instance IDs, with a bit of additional data that enables
/// removing a path and all of its child paths from the tree more quickly.
#[derive(Debug, Serialize)]
pub struct PathMap<T> {
nodes: HashMap<PathBuf, PathMapNode<T>>,
}
impl<T> PathMap<T> {
pub fn new() -> PathMap<T> {
PathMap {
nodes: HashMap::new(),
}
}
pub fn get(&self, path: &Path) -> Option<&T> {
self.nodes.get(path).map(|v| &v.value)
}
pub fn insert(&mut self, path: PathBuf, value: T) {
if let Some(parent_path) = path.parent() {
if let Some(parent) = self.nodes.get_mut(parent_path) {
parent.children.insert(path.to_path_buf());
}
}
self.nodes.insert(path, PathMapNode {
value,
children: HashSet::new(),
});
}
pub fn remove(&mut self, root_path: &Path) -> Option<T> {
if let Some(parent_path) = root_path.parent() {
if let Some(parent) = self.nodes.get_mut(parent_path) {
parent.children.remove(root_path);
}
}
let mut root_node = match self.nodes.remove(root_path) {
Some(node) => node,
None => return None,
};
let root_value = root_node.value;
let mut to_visit: Vec<PathBuf> = root_node.children.drain().collect();
while let Some(path) = to_visit.pop() {
match self.nodes.remove(&path) {
Some(mut node) => {
for child in node.children.drain() {
to_visit.push(child);
}
},
None => {
warn!("Consistency issue; tried to remove {} but it was already removed", path.display());
},
}
}
Some(root_value)
}
pub fn descend(&self, start_path: &Path, target_path: &Path) -> PathBuf {
let relative_path = target_path.strip_prefix(start_path)
.expect("target_path did not begin with start_path");
let mut current_path = start_path.to_path_buf();
for component in relative_path.components() {
match component {
path::Component::Normal(name) => {
let next_path = current_path.join(name);
if self.nodes.contains_key(&next_path) {
current_path = next_path;
} else {
return current_path;
}
},
_ => unreachable!(),
}
}
current_path
}
}

120
server/src/pathext.rs Normal file
View File

@@ -0,0 +1,120 @@
use std::env::current_dir;
use std::path::{Component, Path, PathBuf};
/// Converts a path to a 'route', used as the paths in Rojo.
pub fn path_to_route<A, B>(root: A, value: B) -> Option<Vec<String>>
where
A: AsRef<Path>,
B: AsRef<Path>,
{
let root = root.as_ref();
let value = value.as_ref();
let relative = match value.strip_prefix(root) {
Ok(v) => v,
Err(_) => return None,
};
let result = relative
.components()
.map(|component| {
component.as_os_str().to_string_lossy().into_owned()
})
.collect::<Vec<_>>();
Some(result)
}
#[test]
fn test_path_to_route() {
fn t(root: &Path, value: &Path, result: Option<Vec<String>>) {
assert_eq!(path_to_route(root, value), result);
}
t(
Path::new("/a/b/c"),
Path::new("/a/b/c/d"),
Some(vec!["d".to_string()]),
);
t(Path::new("/a/b"), Path::new("a"), None);
}
#[test]
#[cfg(target_os = "windows")]
fn test_path_to_route_windows() {
fn t(root: &Path, value: &Path, result: Option<Vec<String>>) {
assert_eq!(path_to_route(root, value), result);
}
t(
Path::new("C:\\foo"),
Path::new("C:\\foo\\bar\\baz"),
Some(vec!["bar".to_string(), "baz".to_string()]),
);
}
/// Turns the path into an absolute one, using the current working directory if
/// necessary.
pub fn canonicalish<T: AsRef<Path>>(value: T) -> PathBuf {
let cwd = current_dir().unwrap();
absoluteify(&cwd, value)
}
/// Converts the given path to be absolute if it isn't already using a given
/// root.
pub fn absoluteify<A, B>(root: A, value: B) -> PathBuf
where
A: AsRef<Path>,
B: AsRef<Path>,
{
let root = root.as_ref();
let value = value.as_ref();
if value.is_absolute() {
PathBuf::from(value)
} else {
root.join(value)
}
}
/// Collapses any `.` values along with any `..` values not at the start of the
/// path.
pub fn collapse<T: AsRef<Path>>(value: T) -> PathBuf {
let value = value.as_ref();
let mut buffer = Vec::new();
for component in value.components() {
match component {
Component::ParentDir => match buffer.pop() {
Some(_) => {},
None => buffer.push(component.as_os_str()),
},
Component::CurDir => {},
_ => {
buffer.push(component.as_os_str());
},
}
}
buffer.iter().fold(PathBuf::new(), |mut acc, &x| {
acc.push(x);
acc
})
}
#[test]
fn test_collapse() {
fn identity(buf: PathBuf) {
assert_eq!(buf, collapse(&buf));
}
identity(PathBuf::from("C:\\foo\\bar"));
identity(PathBuf::from("/a/b/c"));
identity(PathBuf::from("a/b"));
assert_eq!(collapse(PathBuf::from("a/b/..")), PathBuf::from("a"));
assert_eq!(collapse(PathBuf::from("./a/b/c/..")), PathBuf::from("a/b"));
assert_eq!(collapse(PathBuf::from("../a")), PathBuf::from("../a"));
}

60
server/src/plugin.rs Normal file
View File

@@ -0,0 +1,60 @@
use rbx::RbxInstance;
use vfs::VfsItem;
use core::Route;
pub enum TransformFileResult {
Value(Option<RbxInstance>),
Pass,
// TODO: Error case
}
pub enum FileChangeResult {
MarkChanged(Option<Vec<Route>>),
Pass,
}
pub trait Plugin {
/// Invoked when a file is read from the filesystem and needs to be turned
/// into a Roblox instance.
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult;
/// Invoked when a file changes on the filesystem. The result defines what
/// routes are marked as needing to be refreshed.
fn handle_file_change(&self, route: &Route) -> FileChangeResult;
}
/// A set of plugins that are composed in order.
pub struct PluginChain {
plugins: Vec<Box<Plugin + Send + Sync>>,
}
impl PluginChain {
pub fn new(plugins: Vec<Box<Plugin + Send + Sync>>) -> PluginChain {
PluginChain {
plugins,
}
}
pub fn transform_file(&self, vfs_item: &VfsItem) -> Option<RbxInstance> {
for plugin in &self.plugins {
match plugin.transform_file(self, vfs_item) {
TransformFileResult::Value(rbx_item) => return rbx_item,
TransformFileResult::Pass => {},
}
}
None
}
pub fn handle_file_change(&self, route: &Route) -> Option<Vec<Route>> {
for plugin in &self.plugins {
match plugin.handle_file_change(route) {
FileChangeResult::MarkChanged(changes) => return changes,
FileChangeResult::Pass => {},
}
}
None
}
}

View File

@@ -0,0 +1,63 @@
use std::collections::HashMap;
use core::Route;
use plugin::{Plugin, PluginChain, TransformFileResult, FileChangeResult};
use rbx::{RbxInstance, RbxValue};
use vfs::VfsItem;
/// A plugin with simple transforms:
/// * Directories become Folder instances
/// * Files become StringValue objects with 'Value' as their contents
pub struct DefaultPlugin;
impl DefaultPlugin {
pub fn new() -> DefaultPlugin {
DefaultPlugin
}
}
impl Plugin for DefaultPlugin {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item {
&VfsItem::File { ref contents, .. } => {
let mut properties = HashMap::new();
properties.insert("Value".to_string(), RbxValue::String {
value: contents.clone(),
});
TransformFileResult::Value(Some(RbxInstance {
name: vfs_item.name().clone(),
class_name: "StringValue".to_string(),
children: Vec::new(),
properties,
route: Some(vfs_item.route().to_vec()),
}))
},
&VfsItem::Dir { ref children, .. } => {
let mut rbx_children = Vec::new();
for (_, child_item) in children {
match plugins.transform_file(child_item) {
Some(rbx_item) => {
rbx_children.push(rbx_item);
},
_ => {},
}
}
TransformFileResult::Value(Some(RbxInstance {
name: vfs_item.name().clone(),
class_name: "*".to_string(),
children: rbx_children,
properties: HashMap::new(),
route: Some(vfs_item.route().to_vec()),
}))
},
}
}
fn handle_file_change(&self, route: &Route) -> FileChangeResult {
FileChangeResult::MarkChanged(Some(vec![route.clone()]))
}
}

View File

@@ -0,0 +1,99 @@
use regex::Regex;
use serde_json;
use core::Route;
use plugin::{Plugin, PluginChain, TransformFileResult, FileChangeResult};
use rbx::RbxInstance;
use vfs::VfsItem;
lazy_static! {
static ref JSON_MODEL_PATTERN: Regex = Regex::new(r"^(.*?)\.model\.json$").unwrap();
}
static JSON_MODEL_INIT: &'static str = "init.model.json";
pub struct JsonModelPlugin;
impl JsonModelPlugin {
pub fn new() -> JsonModelPlugin {
JsonModelPlugin
}
}
impl Plugin for JsonModelPlugin {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item {
&VfsItem::File { ref contents, .. } => {
let rbx_name = match JSON_MODEL_PATTERN.captures(vfs_item.name()) {
Some(captures) => captures.get(1).unwrap().as_str().to_string(),
None => return TransformFileResult::Pass,
};
let mut rbx_item: RbxInstance = match serde_json::from_str(contents) {
Ok(v) => v,
Err(e) => {
eprintln!("Unable to parse JSON Model File named {}: {}", vfs_item.name(), e);
return TransformFileResult::Pass; // This should be an error in the future!
},
};
rbx_item.route = Some(vfs_item.route().to_vec());
rbx_item.name = rbx_name;
TransformFileResult::Value(Some(rbx_item))
},
&VfsItem::Dir { ref children, .. } => {
let init_item = match children.get(JSON_MODEL_INIT) {
Some(v) => v,
None => return TransformFileResult::Pass,
};
let mut rbx_item = match self.transform_file(plugins, init_item) {
TransformFileResult::Value(Some(item)) => item,
TransformFileResult::Value(None) | TransformFileResult::Pass => {
eprintln!("Inconsistency detected in JsonModelPlugin!");
return TransformFileResult::Pass;
},
};
rbx_item.name.clear();
rbx_item.name.push_str(vfs_item.name());
rbx_item.route = Some(vfs_item.route().to_vec());
for (child_name, child_item) in children {
if child_name == init_item.name() {
continue;
}
match plugins.transform_file(child_item) {
Some(child_rbx_item) => {
rbx_item.children.push(child_rbx_item);
},
_ => {},
}
}
TransformFileResult::Value(Some(rbx_item))
},
}
}
fn handle_file_change(&self, route: &Route) -> FileChangeResult {
let leaf = match route.last() {
Some(v) => v,
None => return FileChangeResult::Pass,
};
let is_init = leaf == JSON_MODEL_INIT;
if is_init {
let mut changed = route.clone();
changed.pop();
FileChangeResult::MarkChanged(Some(vec![changed]))
} else {
FileChangeResult::Pass
}
}
}

View File

@@ -0,0 +1,7 @@
mod default_plugin;
mod script_plugin;
mod json_model_plugin;
pub use self::default_plugin::*;
pub use self::script_plugin::*;
pub use self::json_model_plugin::*;

View File

@@ -0,0 +1,121 @@
use std::collections::HashMap;
use regex::Regex;
use core::Route;
use plugin::{Plugin, PluginChain, TransformFileResult, FileChangeResult};
use rbx::{RbxInstance, RbxValue};
use vfs::VfsItem;
lazy_static! {
static ref SERVER_PATTERN: Regex = Regex::new(r"^(.*?)\.server\.lua$").unwrap();
static ref CLIENT_PATTERN: Regex = Regex::new(r"^(.*?)\.client\.lua$").unwrap();
static ref MODULE_PATTERN: Regex = Regex::new(r"^(.*?)\.lua$").unwrap();
}
static SERVER_INIT: &'static str = "init.server.lua";
static CLIENT_INIT: &'static str = "init.client.lua";
static MODULE_INIT: &'static str = "init.lua";
pub struct ScriptPlugin;
impl ScriptPlugin {
pub fn new() -> ScriptPlugin {
ScriptPlugin
}
}
impl Plugin for ScriptPlugin {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item {
&VfsItem::File { ref contents, .. } => {
let name = vfs_item.name();
let (class_name, rbx_name) = {
if let Some(captures) = SERVER_PATTERN.captures(name) {
("Script".to_string(), captures.get(1).unwrap().as_str().to_string())
} else if let Some(captures) = CLIENT_PATTERN.captures(name) {
("LocalScript".to_string(), captures.get(1).unwrap().as_str().to_string())
} else if let Some(captures) = MODULE_PATTERN.captures(name) {
("ModuleScript".to_string(), captures.get(1).unwrap().as_str().to_string())
} else {
return TransformFileResult::Pass;
}
};
let mut properties = HashMap::new();
properties.insert("Source".to_string(), RbxValue::String {
value: contents.clone(),
});
TransformFileResult::Value(Some(RbxInstance {
name: rbx_name,
class_name: class_name,
children: Vec::new(),
properties,
route: Some(vfs_item.route().to_vec()),
}))
},
&VfsItem::Dir { ref children, .. } => {
let init_item = {
let maybe_item = children.get(SERVER_INIT)
.or(children.get(CLIENT_INIT))
.or(children.get(MODULE_INIT));
match maybe_item {
Some(v) => v,
None => return TransformFileResult::Pass,
}
};
let mut rbx_item = match self.transform_file(plugins, init_item) {
TransformFileResult::Value(Some(item)) => item,
_ => {
eprintln!("Inconsistency detected in ScriptPlugin!");
return TransformFileResult::Pass;
},
};
rbx_item.name.clear();
rbx_item.name.push_str(vfs_item.name());
rbx_item.route = Some(vfs_item.route().to_vec());
for (child_name, child_item) in children {
if child_name == init_item.name() {
continue;
}
match plugins.transform_file(child_item) {
Some(child_rbx_item) => {
rbx_item.children.push(child_rbx_item);
},
_ => {},
}
}
TransformFileResult::Value(Some(rbx_item))
},
}
}
fn handle_file_change(&self, route: &Route) -> FileChangeResult {
let leaf = match route.last() {
Some(v) => v,
None => return FileChangeResult::Pass,
};
let is_init = leaf == SERVER_INIT
|| leaf == CLIENT_INIT
|| leaf == MODULE_INIT;
if is_init {
let mut changed = route.clone();
changed.pop();
FileChangeResult::MarkChanged(Some(vec![changed]))
} else {
FileChangeResult::Pass
}
}
}

View File

@@ -1,400 +1,165 @@
use std::{
collections::{HashMap, HashSet},
fmt,
fs::{self, File},
io,
path::{Path, PathBuf},
};
use std::collections::HashMap;
use std::fmt;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::Path;
use maplit::hashmap;
use failure::Fail;
use rbx_tree::RbxValue;
use serde_json;
pub static PROJECT_FILENAME: &'static str = "roblox-project.json";
pub static PROJECT_FILENAME: &'static str = "rojo.json";
// Serde is silly.
const fn yeah() -> bool {
true
#[derive(Debug)]
pub enum ProjectLoadError {
DidNotExist,
FailedToOpen,
FailedToRead,
InvalidJson(serde_json::Error),
}
const fn is_true(value: &bool) -> bool {
*value
#[derive(Debug)]
pub enum ProjectSaveError {
FailedToCreate,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum SourceProjectNode {
Instance {
#[serde(rename = "$className")]
class_name: String,
#[serde(rename = "$properties", default = "HashMap::new", skip_serializing_if = "HashMap::is_empty")]
properties: HashMap<String, RbxValue>,
#[serde(rename = "$ignoreUnknownInstances", default = "yeah", skip_serializing_if = "is_true")]
ignore_unknown_instances: bool,
#[serde(flatten)]
children: HashMap<String, SourceProjectNode>,
},
SyncPoint {
#[serde(rename = "$path")]
path: String,
}
}
impl SourceProjectNode {
pub fn into_project_node(self, project_file_location: &Path) -> ProjectNode {
match self {
SourceProjectNode::Instance { class_name, mut children, properties, ignore_unknown_instances } => {
let mut new_children = HashMap::new();
for (node_name, node) in children.drain() {
new_children.insert(node_name, node.into_project_node(project_file_location));
}
ProjectNode::Instance(InstanceProjectNode {
class_name,
children: new_children,
properties,
metadata: InstanceProjectNodeMetadata {
ignore_unknown_instances,
},
})
},
SourceProjectNode::SyncPoint { path: source_path } => {
let path = if Path::new(&source_path).is_absolute() {
PathBuf::from(source_path)
} else {
let project_folder_location = project_file_location.parent().unwrap();
project_folder_location.join(source_path)
};
ProjectNode::SyncPoint(SyncPointProjectNode {
path,
})
},
}
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SourceProject {
name: String,
tree: SourceProjectNode,
#[serde(skip_serializing_if = "Option::is_none")]
serve_port: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
serve_place_ids: Option<HashSet<u64>>,
}
impl SourceProject {
pub fn into_project(self, project_file_location: &Path) -> Project {
let tree = self.tree.into_project_node(project_file_location);
Project {
name: self.name,
tree,
serve_port: self.serve_port,
serve_place_ids: self.serve_place_ids,
file_location: PathBuf::from(project_file_location),
}
}
}
#[derive(Debug, Fail)]
pub enum ProjectLoadExactError {
#[fail(display = "IO error: {}", _0)]
IoError(#[fail(cause)] io::Error),
#[fail(display = "JSON error: {}", _0)]
JsonError(#[fail(cause)] serde_json::Error),
}
#[derive(Debug, Fail)]
pub enum ProjectLoadFuzzyError {
#[fail(display = "Project not found")]
NotFound,
#[fail(display = "IO error: {}", _0)]
IoError(#[fail(cause)] io::Error),
#[fail(display = "JSON error: {}", _0)]
JsonError(#[fail(cause)] serde_json::Error),
}
impl From<ProjectLoadExactError> for ProjectLoadFuzzyError {
fn from(error: ProjectLoadExactError) -> ProjectLoadFuzzyError {
match error {
ProjectLoadExactError::IoError(inner) => ProjectLoadFuzzyError::IoError(inner),
ProjectLoadExactError::JsonError(inner) => ProjectLoadFuzzyError::JsonError(inner),
}
}
}
#[derive(Debug, Fail)]
#[derive(Debug)]
pub enum ProjectInitError {
AlreadyExists(PathBuf),
IoError(#[fail(cause)] io::Error),
SaveError(#[fail(cause)] ProjectSaveError),
AlreadyExists,
FailedToCreate,
FailedToWrite,
}
impl fmt::Display for ProjectInitError {
fn fmt(&self, output: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ProjectInitError::AlreadyExists(path) => write!(output, "Path {} already exists", path.display()),
ProjectInitError::IoError(inner) => write!(output, "IO error: {}", inner),
ProjectInitError::SaveError(inner) => write!(output, "{}", inner),
}
}
}
#[derive(Debug, Fail)]
pub enum ProjectSaveError {
#[fail(display = "JSON error: {}", _0)]
JsonError(#[fail(cause)] serde_json::Error),
#[fail(display = "IO error: {}", _0)]
IoError(#[fail(cause)] io::Error),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InstanceProjectNodeMetadata {
pub ignore_unknown_instances: bool,
}
impl Default for InstanceProjectNodeMetadata {
fn default() -> InstanceProjectNodeMetadata {
InstanceProjectNodeMetadata {
ignore_unknown_instances: true,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ProjectNode {
Instance(InstanceProjectNode),
SyncPoint(SyncPointProjectNode),
}
impl ProjectNode {
fn to_source_node(&self, project_file_location: &Path) -> SourceProjectNode {
match self {
ProjectNode::Instance(node) => {
let mut children = HashMap::new();
for (key, child) in &node.children {
children.insert(key.clone(), child.to_source_node(project_file_location));
}
SourceProjectNode::Instance {
class_name: node.class_name.clone(),
children,
properties: node.properties.clone(),
ignore_unknown_instances: node.metadata.ignore_unknown_instances,
}
&ProjectInitError::AlreadyExists => {
write!(f, "A project already exists at that location.")
},
ProjectNode::SyncPoint(sync_node) => {
let project_folder_location = project_file_location.parent().unwrap();
let friendly_path = match sync_node.path.strip_prefix(project_folder_location) {
Ok(stripped) => stripped.to_str().unwrap().replace("\\", "/"),
Err(_) => format!("{}", sync_node.path.display()),
};
SourceProjectNode::SyncPoint {
path: friendly_path,
}
&ProjectInitError::FailedToCreate | &ProjectInitError::FailedToWrite => {
write!(f, "Failed to write to the given location.")
},
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InstanceProjectNode {
pub class_name: String,
pub children: HashMap<String, ProjectNode>,
pub properties: HashMap<String, RbxValue>,
pub metadata: InstanceProjectNodeMetadata,
pub struct ProjectPartition {
/// A slash-separated path to a file or folder, relative to the project's
/// directory.
pub path: String,
/// A dot-separated route to a Roblox instance, relative to game.
pub target: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SyncPointProjectNode {
pub path: PathBuf,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
/// Represents a project configured by a user for use with Rojo. Holds anything
/// that can be configured with `rojo.json`.
///
/// In the future, this object will hold dependency information and other handy
/// configurables
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Project {
pub name: String,
pub tree: ProjectNode,
pub serve_port: Option<u16>,
pub serve_place_ids: Option<HashSet<u64>>,
pub file_location: PathBuf,
pub serve_port: u64,
pub partitions: HashMap<String, ProjectPartition>,
}
impl Project {
pub fn init_place(project_fuzzy_path: &Path) -> Result<PathBuf, ProjectInitError> {
let project_path = Project::init_pick_path(project_fuzzy_path)?;
let project_folder_path = project_path.parent().unwrap();
let project_name = if project_fuzzy_path == project_path {
project_fuzzy_path.parent().unwrap().file_name().unwrap().to_str().unwrap()
} else {
project_fuzzy_path.file_name().unwrap().to_str().unwrap()
};
let tree = ProjectNode::Instance(InstanceProjectNode {
class_name: "DataModel".to_string(),
children: hashmap! {
String::from("ReplicatedStorage") => ProjectNode::Instance(InstanceProjectNode {
class_name: String::from("ReplicatedStorage"),
children: hashmap! {
String::from("Source") => ProjectNode::SyncPoint(SyncPointProjectNode {
path: project_folder_path.join("src"),
}),
},
properties: HashMap::new(),
metadata: Default::default(),
}),
String::from("HttpService") => ProjectNode::Instance(InstanceProjectNode {
class_name: String::from("HttpService"),
children: HashMap::new(),
properties: hashmap! {
String::from("HttpEnabled") => RbxValue::Bool {
value: true,
},
},
metadata: Default::default(),
}),
},
properties: HashMap::new(),
metadata: Default::default(),
});
let project = Project {
name: project_name.to_string(),
tree,
serve_port: None,
serve_place_ids: None,
file_location: project_path.clone(),
};
project.save()
.map_err(ProjectInitError::SaveError)?;
Ok(project_path)
}
pub fn init_model(project_fuzzy_path: &Path) -> Result<PathBuf, ProjectInitError> {
let project_path = Project::init_pick_path(project_fuzzy_path)?;
let project_folder_path = project_path.parent().unwrap();
let project_name = if project_fuzzy_path == project_path {
project_fuzzy_path.parent().unwrap().file_name().unwrap().to_str().unwrap()
} else {
project_fuzzy_path.file_name().unwrap().to_str().unwrap()
};
let tree = ProjectNode::SyncPoint(SyncPointProjectNode {
path: project_folder_path.join("src"),
});
let project = Project {
name: project_name.to_string(),
tree,
serve_port: None,
serve_place_ids: None,
file_location: project_path.clone(),
};
project.save()
.map_err(ProjectInitError::SaveError)?;
Ok(project_path)
}
fn init_pick_path(project_fuzzy_path: &Path) -> Result<PathBuf, ProjectInitError> {
let is_exact = project_fuzzy_path.extension().is_some();
let project_path = if is_exact {
project_fuzzy_path.to_path_buf()
} else {
project_fuzzy_path.join(PROJECT_FILENAME)
};
match fs::metadata(&project_path) {
Err(error) => match error.kind() {
io::ErrorKind::NotFound => {},
_ => return Err(ProjectInitError::IoError(error)),
},
Ok(_) => return Err(ProjectInitError::AlreadyExists(project_path)),
}
Ok(project_path)
}
pub fn locate(start_location: &Path) -> Option<PathBuf> {
// TODO: Check for specific error kinds, convert 'not found' to Result.
let location_metadata = fs::metadata(start_location).ok()?;
// If this is a file, we should assume it's the config we want
if location_metadata.is_file() {
return Some(start_location.to_path_buf());
} else if location_metadata.is_dir() {
let with_file = start_location.join(PROJECT_FILENAME);
if let Ok(with_file_metadata) = fs::metadata(&with_file) {
if with_file_metadata.is_file() {
return Some(with_file);
} else {
return None;
}
}
}
match start_location.parent() {
Some(parent_location) => Self::locate(parent_location),
None => None,
/// Creates a new empty Project object with the given name.
pub fn new<T: Into<String>>(name: T) -> Project {
Project {
name: name.into(),
..Default::default()
}
}
pub fn load_fuzzy(fuzzy_project_location: &Path) -> Result<Project, ProjectLoadFuzzyError> {
let project_path = Self::locate(fuzzy_project_location)
.ok_or(ProjectLoadFuzzyError::NotFound)?;
/// Initializes a new project inside the given folder path.
pub fn init<T: AsRef<Path>>(location: T) -> Result<Project, ProjectInitError> {
let location = location.as_ref();
let package_path = location.join(PROJECT_FILENAME);
Self::load_exact(&project_path).map_err(From::from)
// We abort if the project file already exists.
match fs::metadata(&package_path) {
Ok(_) => return Err(ProjectInitError::AlreadyExists),
Err(_) => {},
}
let mut file = match File::create(&package_path) {
Ok(f) => f,
Err(_) => return Err(ProjectInitError::FailedToCreate),
};
// Try to give the project a meaningful name.
// If we can't, we'll just fall back to a default.
let name = match location.file_name() {
Some(v) => v.to_string_lossy().into_owned(),
None => "new-project".to_string(),
};
// Configure the project with all of the values we know so far.
let project = Project::new(name);
let serialized = serde_json::to_string_pretty(&project).unwrap();
match file.write(serialized.as_bytes()) {
Ok(_) => {},
Err(_) => return Err(ProjectInitError::FailedToWrite),
}
Ok(project)
}
pub fn load_exact(project_file_location: &Path) -> Result<Project, ProjectLoadExactError> {
let contents = fs::read_to_string(project_file_location)
.map_err(ProjectLoadExactError::IoError)?;
/// Attempts to load a project from the file named PROJECT_FILENAME from the
/// given folder.
pub fn load<T: AsRef<Path>>(location: T) -> Result<Project, ProjectLoadError> {
let package_path = location.as_ref().join(Path::new(PROJECT_FILENAME));
let parsed: SourceProject = serde_json::from_str(&contents)
.map_err(ProjectLoadExactError::JsonError)?;
match fs::metadata(&package_path) {
Ok(_) => {},
Err(_) => return Err(ProjectLoadError::DidNotExist),
}
Ok(parsed.into_project(project_file_location))
let mut file = match File::open(&package_path) {
Ok(f) => f,
Err(_) => return Err(ProjectLoadError::FailedToOpen),
};
let mut contents = String::new();
match file.read_to_string(&mut contents) {
Ok(_) => {},
Err(_) => return Err(ProjectLoadError::FailedToRead),
}
match serde_json::from_str(&contents) {
Ok(v) => Ok(v),
Err(e) => return Err(ProjectLoadError::InvalidJson(e)),
}
}
pub fn save(&self) -> Result<(), ProjectSaveError> {
let source_project = self.to_source_project();
let mut file = File::create(&self.file_location)
.map_err(ProjectSaveError::IoError)?;
/// Saves the given project file to the given folder with the appropriate name.
pub fn save<T: AsRef<Path>>(&self, location: T) -> Result<(), ProjectSaveError> {
let package_path = location.as_ref().join(Path::new(PROJECT_FILENAME));
serde_json::to_writer_pretty(&mut file, &source_project)
.map_err(ProjectSaveError::JsonError)?;
let mut file = match File::create(&package_path) {
Ok(f) => f,
Err(_) => return Err(ProjectSaveError::FailedToCreate),
};
let serialized = serde_json::to_string_pretty(self).unwrap();
file.write(serialized.as_bytes()).unwrap();
Ok(())
}
}
fn to_source_project(&self) -> SourceProject {
SourceProject {
name: self.name.clone(),
tree: self.tree.to_source_node(&self.file_location),
serve_port: self.serve_port,
serve_place_ids: self.serve_place_ids.clone(),
impl Default for Project {
fn default() -> Project {
Project {
name: "new-project".to_string(),
serve_port: 8000,
partitions: HashMap::new(),
}
}
}
}

38
server/src/rbx.rs Normal file
View File

@@ -0,0 +1,38 @@
use std::collections::HashMap;
/// Represents data about a Roblox instance
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct RbxInstance {
pub name: String,
pub class_name: String,
#[serde(default = "Vec::new")]
pub children: Vec<RbxInstance>,
#[serde(default = "HashMap::new")]
pub properties: HashMap<String, RbxValue>,
/// The route that this instance was generated from, if there was one.
pub route: Option<Vec<String>>,
}
/// Any kind value that can be used by Roblox
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase", tag = "Type")]
pub enum RbxValue {
#[serde(rename_all = "PascalCase")]
String {
value: String,
},
#[serde(rename_all = "PascalCase")]
Bool {
value: bool,
},
#[serde(rename_all = "PascalCase")]
Number {
value: f64,
},
// TODO: Compound types like Vector3
}

View File

@@ -1,539 +0,0 @@
use std::{
borrow::Cow,
collections::HashMap,
fmt,
path::{Path, PathBuf},
str,
sync::{Arc, Mutex},
};
use failure::Fail;
use rbx_tree::{RbxTree, RbxInstanceProperties, RbxValue, RbxId};
use crate::{
project::{Project, ProjectNode, InstanceProjectNodeMetadata},
message_queue::MessageQueue,
imfs::{Imfs, ImfsItem, ImfsFile},
path_map::PathMap,
rbx_snapshot::{RbxSnapshotInstance, InstanceChanges, snapshot_from_tree, reify_root, reconcile_subtree},
};
const INIT_SCRIPT: &str = "init.lua";
const INIT_SERVER_SCRIPT: &str = "init.server.lua";
const INIT_CLIENT_SCRIPT: &str = "init.client.lua";
pub struct RbxSession {
tree: RbxTree,
path_map: PathMap<RbxId>,
instance_metadata_map: HashMap<RbxId, InstanceProjectNodeMetadata>,
sync_point_names: HashMap<PathBuf, String>,
message_queue: Arc<MessageQueue<InstanceChanges>>,
imfs: Arc<Mutex<Imfs>>,
}
impl RbxSession {
pub fn new(
project: Arc<Project>,
imfs: Arc<Mutex<Imfs>>,
message_queue: Arc<MessageQueue<InstanceChanges>>,
) -> RbxSession {
let mut sync_point_names = HashMap::new();
let mut path_map = PathMap::new();
let mut instance_metadata_map = HashMap::new();
let tree = {
let temp_imfs = imfs.lock().unwrap();
construct_initial_tree(&project, &temp_imfs, &mut path_map, &mut instance_metadata_map, &mut sync_point_names)
};
RbxSession {
tree,
path_map,
instance_metadata_map,
sync_point_names,
message_queue,
imfs,
}
}
fn path_created_or_updated(&mut self, path: &Path) {
// TODO: Track paths actually updated in each step so we can ignore
// redundant changes.
let mut changes = InstanceChanges::default();
{
let imfs = self.imfs.lock().unwrap();
let root_path = imfs.get_root_for_path(path)
.expect("Path was outside in-memory filesystem roots");
// Find the closest instance in the tree that currently exists
let mut path_to_snapshot = self.path_map.descend(root_path, path);
let &instance_id = self.path_map.get(&path_to_snapshot).unwrap();
// If this is a file that might affect its parent if modified, we
// should snapshot its parent instead.
match path_to_snapshot.file_name().unwrap().to_str() {
Some(INIT_SCRIPT) | Some(INIT_SERVER_SCRIPT) | Some(INIT_CLIENT_SCRIPT) => {
path_to_snapshot.pop();
},
_ => {},
}
trace!("Snapshotting path {}", path_to_snapshot.display());
let maybe_snapshot = snapshot_instances_from_imfs(&imfs, &path_to_snapshot, &mut self.sync_point_names)
.unwrap_or_else(|_| panic!("Could not generate instance snapshot for path {}", path_to_snapshot.display()));
let snapshot = match maybe_snapshot {
Some(snapshot) => snapshot,
None => {
trace!("Path resulted in no snapshot being generated.");
return;
},
};
trace!("Snapshot: {:#?}", snapshot);
reconcile_subtree(
&mut self.tree,
instance_id,
&snapshot,
&mut self.path_map,
&mut self.instance_metadata_map,
&mut changes,
);
}
if changes.is_empty() {
trace!("No instance changes triggered from file update.");
} else {
trace!("Pushing changes: {}", changes);
self.message_queue.push_messages(&[changes]);
}
}
pub fn path_created(&mut self, path: &Path) {
info!("Path created: {}", path.display());
self.path_created_or_updated(path);
}
pub fn path_updated(&mut self, path: &Path) {
info!("Path updated: {}", path.display());
{
let imfs = self.imfs.lock().unwrap();
// If the path doesn't exist or is a directory, we don't care if it
// updated
match imfs.get(path) {
Some(ImfsItem::Directory(_)) | None => {
trace!("Updated path was a directory, ignoring.");
return;
},
Some(ImfsItem::File(_)) => {},
}
}
self.path_created_or_updated(path);
}
pub fn path_removed(&mut self, path: &Path) {
info!("Path removed: {}", path.display());
self.path_map.remove(path);
self.path_created_or_updated(path);
}
pub fn path_renamed(&mut self, from_path: &Path, to_path: &Path) {
info!("Path renamed from {} to {}", from_path.display(), to_path.display());
self.path_map.remove(from_path);
self.path_created_or_updated(from_path);
self.path_created_or_updated(to_path);
}
pub fn get_tree(&self) -> &RbxTree {
&self.tree
}
pub fn get_instance_metadata(&self, id: RbxId) -> Option<&InstanceProjectNodeMetadata> {
self.instance_metadata_map.get(&id)
}
pub fn debug_get_path_map(&self) -> &PathMap<RbxId> {
&self.path_map
}
}
pub fn construct_oneoff_tree(project: &Project, imfs: &Imfs) -> RbxTree {
let mut path_map = PathMap::new();
let mut instance_metadata_map = HashMap::new();
let mut sync_point_names = HashMap::new();
construct_initial_tree(project, imfs, &mut path_map, &mut instance_metadata_map, &mut sync_point_names)
}
fn construct_initial_tree(
project: &Project,
imfs: &Imfs,
path_map: &mut PathMap<RbxId>,
instance_metadata_map: &mut HashMap<RbxId, InstanceProjectNodeMetadata>,
sync_point_names: &mut HashMap<PathBuf, String>,
) -> RbxTree {
let snapshot = construct_project_node(
imfs,
&project.name,
&project.tree,
sync_point_names,
);
let mut changes = InstanceChanges::default();
let tree = reify_root(&snapshot, path_map, instance_metadata_map, &mut changes);
tree
}
fn construct_project_node<'a>(
imfs: &'a Imfs,
instance_name: &'a str,
project_node: &'a ProjectNode,
sync_point_names: &mut HashMap<PathBuf, String>,
) -> RbxSnapshotInstance<'a> {
match project_node {
ProjectNode::Instance(node) => {
let mut children = Vec::new();
for (child_name, child_project_node) in &node.children {
children.push(construct_project_node(imfs, child_name, child_project_node, sync_point_names));
}
RbxSnapshotInstance {
class_name: Cow::Borrowed(&node.class_name),
name: Cow::Borrowed(instance_name),
properties: node.properties.clone(),
children,
source_path: None,
metadata: Some(node.metadata.clone()),
}
},
ProjectNode::SyncPoint(node) => {
// TODO: Propagate errors upward instead of dying
let mut snapshot = snapshot_instances_from_imfs(imfs, &node.path, sync_point_names)
.expect("Could not reify nodes from Imfs")
.expect("Sync point node did not result in an instance");
snapshot.name = Cow::Borrowed(instance_name);
sync_point_names.insert(node.path.clone(), instance_name.to_string());
snapshot
},
}
}
#[derive(Debug, Clone, Copy)]
enum FileType {
ModuleScript,
ServerScript,
ClientScript,
StringValue,
LocalizationTable,
XmlModel,
BinaryModel,
}
fn get_trailing<'a>(input: &'a str, trailer: &str) -> Option<&'a str> {
if input.ends_with(trailer) {
let end = input.len().saturating_sub(trailer.len());
Some(&input[..end])
} else {
None
}
}
fn classify_file(file: &ImfsFile) -> Option<(&str, FileType)> {
static EXTENSIONS_TO_TYPES: &[(&str, FileType)] = &[
(".server.lua", FileType::ServerScript),
(".client.lua", FileType::ClientScript),
(".lua", FileType::ModuleScript),
(".csv", FileType::LocalizationTable),
(".txt", FileType::StringValue),
(".rbxmx", FileType::XmlModel),
(".rbxm", FileType::BinaryModel),
];
let file_name = file.path.file_name()?.to_str()?;
for (extension, file_type) in EXTENSIONS_TO_TYPES {
if let Some(instance_name) = get_trailing(file_name, extension) {
return Some((instance_name, *file_type))
}
}
None
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct LocalizationEntryCsv {
key: String,
context: String,
example: String,
source: String,
#[serde(flatten)]
values: HashMap<String, String>,
}
impl LocalizationEntryCsv {
fn to_json(self) -> LocalizationEntryJson {
LocalizationEntryJson {
key: self.key,
context: self.context,
example: self.example,
source: self.source,
values: self.values,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct LocalizationEntryJson {
key: String,
context: String,
example: String,
source: String,
values: HashMap<String, String>,
}
#[derive(Debug, Fail)]
enum SnapshotError {
DidNotExist(PathBuf),
// TODO: Add file path to the error message?
Utf8Error {
#[fail(cause)]
inner: str::Utf8Error,
path: PathBuf,
},
XmlModelDecodeError {
inner: rbx_xml::DecodeError,
path: PathBuf,
},
BinaryModelDecodeError {
inner: rbx_binary::DecodeError,
path: PathBuf,
},
}
impl fmt::Display for SnapshotError {
fn fmt(&self, output: &mut fmt::Formatter) -> fmt::Result {
match self {
SnapshotError::DidNotExist(path) => write!(output, "Path did not exist: {}", path.display()),
SnapshotError::Utf8Error { inner, path } => {
write!(output, "Invalid UTF-8: {} in path {}", inner, path.display())
},
SnapshotError::XmlModelDecodeError { inner, path } => {
write!(output, "Malformed rbxmx model: {:?} in path {}", inner, path.display())
},
SnapshotError::BinaryModelDecodeError { inner, path } => {
write!(output, "Malformed rbxm model: {:?} in path {}", inner, path.display())
},
}
}
}
fn snapshot_xml_model<'a>(
instance_name: Cow<'a, str>,
file: &ImfsFile,
) -> Result<Option<RbxSnapshotInstance<'a>>, SnapshotError> {
let mut temp_tree = RbxTree::new(RbxInstanceProperties {
name: "Temp".to_owned(),
class_name: "Folder".to_owned(),
properties: HashMap::new(),
});
let root_id = temp_tree.get_root_id();
rbx_xml::decode(&mut temp_tree, root_id, file.contents.as_slice())
.map_err(|inner| SnapshotError::XmlModelDecodeError {
inner,
path: file.path.clone(),
})?;
let root_instance = temp_tree.get_instance(root_id).unwrap();
let children = root_instance.get_children_ids();
match children.len() {
0 => Ok(None),
1 => {
let mut snapshot = snapshot_from_tree(&temp_tree, children[0]).unwrap();
snapshot.name = instance_name;
Ok(Some(snapshot))
},
_ => panic!("Rojo doesn't have support for model files with multiple roots yet"),
}
}
fn snapshot_binary_model<'a>(
instance_name: Cow<'a, str>,
file: &ImfsFile,
) -> Result<Option<RbxSnapshotInstance<'a>>, SnapshotError> {
let mut temp_tree = RbxTree::new(RbxInstanceProperties {
name: "Temp".to_owned(),
class_name: "Folder".to_owned(),
properties: HashMap::new(),
});
let root_id = temp_tree.get_root_id();
rbx_binary::decode(&mut temp_tree, root_id, file.contents.as_slice())
.map_err(|inner| SnapshotError::BinaryModelDecodeError {
inner,
path: file.path.clone(),
})?;
let root_instance = temp_tree.get_instance(root_id).unwrap();
let children = root_instance.get_children_ids();
match children.len() {
0 => Ok(None),
1 => {
let mut snapshot = snapshot_from_tree(&temp_tree, children[0]).unwrap();
snapshot.name = instance_name;
Ok(Some(snapshot))
},
_ => panic!("Rojo doesn't have support for model files with multiple roots yet"),
}
}
fn snapshot_instances_from_imfs<'a>(
imfs: &'a Imfs,
imfs_path: &Path,
sync_point_names: &HashMap<PathBuf, String>,
) -> Result<Option<RbxSnapshotInstance<'a>>, SnapshotError> {
match imfs.get(imfs_path) {
Some(ImfsItem::File(file)) => {
let (instance_name, file_type) = match classify_file(file) {
Some(info) => info,
None => return Ok(None),
};
let instance_name = if let Some(actual_name) = sync_point_names.get(imfs_path) {
Cow::Owned(actual_name.clone())
} else {
Cow::Borrowed(instance_name)
};
let class_name = match file_type {
FileType::ModuleScript => "ModuleScript",
FileType::ServerScript => "Script",
FileType::ClientScript => "LocalScript",
FileType::StringValue => "StringValue",
FileType::LocalizationTable => "LocalizationTable",
FileType::XmlModel => return snapshot_xml_model(instance_name, file),
FileType::BinaryModel => return snapshot_binary_model(instance_name, file),
};
let contents = str::from_utf8(&file.contents)
.map_err(|inner| SnapshotError::Utf8Error {
inner,
path: imfs_path.to_path_buf(),
})?;
let mut properties = HashMap::new();
match file_type {
FileType::ModuleScript | FileType::ServerScript | FileType::ClientScript => {
properties.insert(String::from("Source"), RbxValue::String {
value: contents.to_string(),
});
},
FileType::StringValue => {
properties.insert(String::from("Value"), RbxValue::String {
value: contents.to_string(),
});
},
FileType::LocalizationTable => {
let entries: Vec<LocalizationEntryJson> = csv::Reader::from_reader(contents.as_bytes())
.deserialize()
.map(|result| result.expect("Malformed localization table found!"))
.map(LocalizationEntryCsv::to_json)
.collect();
let table_contents = serde_json::to_string(&entries)
.expect("Could not encode JSON for localization table");
properties.insert(String::from("Contents"), RbxValue::String {
value: table_contents,
});
},
FileType::XmlModel | FileType::BinaryModel => unreachable!(),
}
Ok(Some(RbxSnapshotInstance {
name: instance_name,
class_name: Cow::Borrowed(class_name),
properties,
children: Vec::new(),
source_path: Some(file.path.clone()),
metadata: None,
}))
},
Some(ImfsItem::Directory(directory)) => {
// TODO: Expand init support to handle server and client scripts
let init_path = directory.path.join(INIT_SCRIPT);
let init_server_path = directory.path.join(INIT_SERVER_SCRIPT);
let init_client_path = directory.path.join(INIT_CLIENT_SCRIPT);
let mut instance = if directory.children.contains(&init_path) {
snapshot_instances_from_imfs(imfs, &init_path, sync_point_names)?
.expect("Could not snapshot instance from file that existed!")
} else if directory.children.contains(&init_server_path) {
snapshot_instances_from_imfs(imfs, &init_server_path, sync_point_names)?
.expect("Could not snapshot instance from file that existed!")
} else if directory.children.contains(&init_client_path) {
snapshot_instances_from_imfs(imfs, &init_client_path, sync_point_names)?
.expect("Could not snapshot instance from file that existed!")
} else {
RbxSnapshotInstance {
class_name: Cow::Borrowed("Folder"),
name: Cow::Borrowed(""),
properties: HashMap::new(),
children: Vec::new(),
source_path: Some(directory.path.clone()),
metadata: None,
}
};
// We have to be careful not to lose instance names that are
// specified in the project manifest. We store them in
// sync_point_names when the original tree is constructed.
instance.name = if let Some(actual_name) = sync_point_names.get(&directory.path) {
Cow::Owned(actual_name.clone())
} else {
Cow::Borrowed(directory.path
.file_name().expect("Could not extract file name")
.to_str().expect("Could not convert path to UTF-8"))
};
for child_path in &directory.children {
match child_path.file_name().unwrap().to_str().unwrap() {
INIT_SCRIPT | INIT_SERVER_SCRIPT | INIT_CLIENT_SCRIPT => {
// The existence of files with these names modifies the
// parent instance and is handled above, so we can skip
// them here.
},
_ => {
match snapshot_instances_from_imfs(imfs, child_path, sync_point_names)? {
Some(child) => {
instance.children.push(child);
},
None => {},
}
},
}
}
Ok(Some(instance))
},
None => Err(SnapshotError::DidNotExist(imfs_path.to_path_buf())),
}
}

View File

@@ -1,307 +0,0 @@
use std::{
str,
borrow::Cow,
collections::{HashMap, HashSet},
fmt,
path::PathBuf,
};
use rbx_tree::{RbxTree, RbxId, RbxInstanceProperties, RbxValue};
use crate::{
path_map::PathMap,
project::InstanceProjectNodeMetadata,
};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct InstanceChanges {
pub added: HashSet<RbxId>,
pub removed: HashSet<RbxId>,
pub updated: HashSet<RbxId>,
}
impl fmt::Display for InstanceChanges {
fn fmt(&self, output: &mut fmt::Formatter) -> fmt::Result {
writeln!(output, "InstanceChanges {{")?;
if !self.added.is_empty() {
writeln!(output, " Added:")?;
for id in &self.added {
writeln!(output, " {}", id)?;
}
}
if !self.removed.is_empty() {
writeln!(output, " Removed:")?;
for id in &self.removed {
writeln!(output, " {}", id)?;
}
}
if !self.updated.is_empty() {
writeln!(output, " Updated:")?;
for id in &self.updated {
writeln!(output, " {}", id)?;
}
}
writeln!(output, "}}")
}
}
impl InstanceChanges {
pub fn is_empty(&self) -> bool {
self.added.is_empty() && self.removed.is_empty() && self.updated.is_empty()
}
}
#[derive(Debug)]
pub struct RbxSnapshotInstance<'a> {
pub name: Cow<'a, str>,
pub class_name: Cow<'a, str>,
pub properties: HashMap<String, RbxValue>,
pub children: Vec<RbxSnapshotInstance<'a>>,
pub source_path: Option<PathBuf>,
pub metadata: Option<InstanceProjectNodeMetadata>,
}
pub fn snapshot_from_tree(tree: &RbxTree, id: RbxId) -> Option<RbxSnapshotInstance<'static>> {
let instance = tree.get_instance(id)?;
let mut children = Vec::new();
for &child_id in instance.get_children_ids() {
children.push(snapshot_from_tree(tree, child_id)?);
}
Some(RbxSnapshotInstance {
name: Cow::Owned(instance.name.to_owned()),
class_name: Cow::Owned(instance.class_name.to_owned()),
properties: instance.properties.clone(),
children,
source_path: None,
metadata: None,
})
}
pub fn reify_root(
snapshot: &RbxSnapshotInstance,
path_map: &mut PathMap<RbxId>,
instance_metadata_map: &mut HashMap<RbxId, InstanceProjectNodeMetadata>,
changes: &mut InstanceChanges,
) -> RbxTree {
let instance = reify_core(snapshot);
let mut tree = RbxTree::new(instance);
let root_id = tree.get_root_id();
if let Some(source_path) = &snapshot.source_path {
path_map.insert(source_path.clone(), root_id);
}
if let Some(metadata) = &snapshot.metadata {
instance_metadata_map.insert(root_id, metadata.clone());
}
changes.added.insert(root_id);
for child in &snapshot.children {
reify_subtree(child, &mut tree, root_id, path_map, instance_metadata_map, changes);
}
tree
}
pub fn reify_subtree(
snapshot: &RbxSnapshotInstance,
tree: &mut RbxTree,
parent_id: RbxId,
path_map: &mut PathMap<RbxId>,
instance_metadata_map: &mut HashMap<RbxId, InstanceProjectNodeMetadata>,
changes: &mut InstanceChanges,
) {
let instance = reify_core(snapshot);
let id = tree.insert_instance(instance, parent_id);
if let Some(source_path) = &snapshot.source_path {
path_map.insert(source_path.clone(), id);
}
if let Some(metadata) = &snapshot.metadata {
instance_metadata_map.insert(id, metadata.clone());
}
changes.added.insert(id);
for child in &snapshot.children {
reify_subtree(child, tree, id, path_map, instance_metadata_map, changes);
}
}
pub fn reconcile_subtree(
tree: &mut RbxTree,
id: RbxId,
snapshot: &RbxSnapshotInstance,
path_map: &mut PathMap<RbxId>,
instance_metadata_map: &mut HashMap<RbxId, InstanceProjectNodeMetadata>,
changes: &mut InstanceChanges,
) {
if let Some(source_path) = &snapshot.source_path {
path_map.insert(source_path.clone(), id);
}
if let Some(metadata) = &snapshot.metadata {
instance_metadata_map.insert(id, metadata.clone());
}
if reconcile_instance_properties(tree.get_instance_mut(id).unwrap(), snapshot) {
changes.updated.insert(id);
}
reconcile_instance_children(tree, id, snapshot, path_map, instance_metadata_map, changes);
}
fn reify_core(snapshot: &RbxSnapshotInstance) -> RbxInstanceProperties {
let mut properties = HashMap::new();
for (key, value) in &snapshot.properties {
properties.insert(key.clone(), value.clone());
}
let instance = RbxInstanceProperties {
name: snapshot.name.to_string(),
class_name: snapshot.class_name.to_string(),
properties,
};
instance
}
fn reconcile_instance_properties(instance: &mut RbxInstanceProperties, snapshot: &RbxSnapshotInstance) -> bool {
let mut has_diffs = false;
if instance.name != snapshot.name {
instance.name = snapshot.name.to_string();
has_diffs = true;
}
if instance.class_name != snapshot.class_name {
instance.class_name = snapshot.class_name.to_string();
has_diffs = true;
}
let mut property_updates = HashMap::new();
for (key, instance_value) in &instance.properties {
match snapshot.properties.get(key) {
Some(snapshot_value) => {
if snapshot_value != instance_value {
property_updates.insert(key.clone(), Some(snapshot_value.clone()));
}
},
None => {
property_updates.insert(key.clone(), None);
},
}
}
for (key, snapshot_value) in &snapshot.properties {
if property_updates.contains_key(key) {
continue;
}
match instance.properties.get(key) {
Some(instance_value) => {
if snapshot_value != instance_value {
property_updates.insert(key.clone(), Some(snapshot_value.clone()));
}
},
None => {
property_updates.insert(key.clone(), Some(snapshot_value.clone()));
},
}
}
has_diffs = has_diffs || !property_updates.is_empty();
for (key, change) in property_updates.drain() {
match change {
Some(value) => instance.properties.insert(key, value),
None => instance.properties.remove(&key),
};
}
has_diffs
}
fn reconcile_instance_children(
tree: &mut RbxTree,
id: RbxId,
snapshot: &RbxSnapshotInstance,
path_map: &mut PathMap<RbxId>,
instance_metadata_map: &mut HashMap<RbxId, InstanceProjectNodeMetadata>,
changes: &mut InstanceChanges,
) {
let mut visited_snapshot_indices = HashSet::new();
let mut children_to_update: Vec<(RbxId, &RbxSnapshotInstance)> = Vec::new();
let mut children_to_add: Vec<&RbxSnapshotInstance> = Vec::new();
let mut children_to_remove: Vec<RbxId> = Vec::new();
let children_ids = tree.get_instance(id).unwrap().get_children_ids();
// Find all instances that were removed or updated, which we derive by
// trying to pair up existing instances to snapshots.
for &child_id in children_ids {
let child_instance = tree.get_instance(child_id).unwrap();
// Locate a matching snapshot for this instance
let mut matching_snapshot = None;
for (snapshot_index, child_snapshot) in snapshot.children.iter().enumerate() {
if visited_snapshot_indices.contains(&snapshot_index) {
continue;
}
// We assume that instances with the same name are probably pretty
// similar. This heuristic is similar to React's reconciliation
// strategy.
if child_snapshot.name == child_instance.name {
visited_snapshot_indices.insert(snapshot_index);
matching_snapshot = Some(child_snapshot);
break;
}
}
match matching_snapshot {
Some(child_snapshot) => {
children_to_update.push((child_instance.get_id(), child_snapshot));
},
None => {
children_to_remove.push(child_instance.get_id());
},
}
}
// Find all instancs that were added, which is just the snapshots we didn't
// match up to existing instances above.
for (snapshot_index, child_snapshot) in snapshot.children.iter().enumerate() {
if !visited_snapshot_indices.contains(&snapshot_index) {
children_to_add.push(child_snapshot);
}
}
for child_snapshot in &children_to_add {
reify_subtree(child_snapshot, tree, id, path_map, instance_metadata_map, changes);
}
for child_id in &children_to_remove {
if let Some(subtree) = tree.remove_instance(*child_id) {
for id in subtree.iter_all_ids() {
instance_metadata_map.remove(&id);
changes.removed.insert(id);
}
}
}
for (child_id, child_snapshot) in &children_to_update {
reconcile_subtree(tree, *child_id, child_snapshot, path_map, instance_metadata_map, changes);
}
}

View File

@@ -1,63 +0,0 @@
//! Interactions with Roblox Studio's installation, including its location and
//! mechanisms like PluginSettings.
#![allow(dead_code)]
use std::path::PathBuf;
#[cfg(all(not(debug_assertions), not(feature = "bundle-plugin")))]
compile_error!("`bundle-plugin` feature must be set for release builds.");
#[cfg(feature = "bundle-plugin")]
static PLUGIN_RBXM: &'static [u8] = include_bytes!("../target/plugin.rbxmx");
#[cfg(target_os = "windows")]
pub fn get_install_location() -> Option<PathBuf> {
use std::env;
let local_app_data = env::var("LocalAppData").ok()?;
let mut location = PathBuf::from(local_app_data);
location.push("Roblox");
Some(location)
}
#[cfg(target_os = "macos")]
pub fn get_install_location() -> Option<PathBuf> {
unimplemented!();
}
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
pub fn get_install_location() -> Option<PathBuf> {
// Roblox Studio doesn't install on any other platforms!
None
}
pub fn get_plugin_location() -> Option<PathBuf> {
let mut location = get_install_location()?;
location.push("Plugins/Rojo.rbxmx");
Some(location)
}
#[cfg(feature = "bundle-plugin")]
pub fn install_bundled_plugin() -> Option<()> {
use std::fs::File;
use std::io::Write;
info!("Installing plugin...");
let mut file = File::create(get_plugin_location()?).ok()?;
file.write_all(PLUGIN_RBXM).ok()?;
Some(())
}
#[cfg(not(feature = "bundle-plugin"))]
pub fn install_bundled_plugin() -> Option<()> {
info!("Skipping plugin installation, bundle-plugin not set.");
Some(())
}

View File

@@ -1,61 +0,0 @@
use std::{
sync::{Arc, Mutex},
io,
};
use crate::{
message_queue::MessageQueue,
project::Project,
imfs::Imfs,
session_id::SessionId,
rbx_session::RbxSession,
rbx_snapshot::InstanceChanges,
fs_watcher::FsWatcher,
};
pub struct Session {
pub project: Arc<Project>,
pub session_id: SessionId,
pub message_queue: Arc<MessageQueue<InstanceChanges>>,
pub rbx_session: Arc<Mutex<RbxSession>>,
pub imfs: Arc<Mutex<Imfs>>,
_fs_watcher: FsWatcher,
}
impl Session {
pub fn new(project: Arc<Project>) -> io::Result<Session> {
let imfs = {
let mut imfs = Imfs::new();
imfs.add_roots_from_project(&project)?;
Arc::new(Mutex::new(imfs))
};
let message_queue = Arc::new(MessageQueue::new());
let rbx_session = Arc::new(Mutex::new(RbxSession::new(
Arc::clone(&project),
Arc::clone(&imfs),
Arc::clone(&message_queue),
)));
let fs_watcher = FsWatcher::start(
Arc::clone(&imfs),
Arc::clone(&rbx_session),
);
let session_id = SessionId::new();
Ok(Session {
project,
session_id,
message_queue,
rbx_session,
imfs,
_fs_watcher: fs_watcher,
})
}
pub fn get_project(&self) -> &Project {
&self.project
}
}

View File

@@ -1,11 +0,0 @@
use serde_derive::{Serialize, Deserialize};
use uuid::Uuid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SessionId(Uuid);
impl SessionId {
pub fn new() -> SessionId {
SessionId(Uuid::new_v4())
}
}

7
server/src/vfs/mod.rs Normal file
View File

@@ -0,0 +1,7 @@
mod vfs_session;
mod vfs_item;
mod vfs_watcher;
pub use self::vfs_session::*;
pub use self::vfs_item::*;
pub use self::vfs_watcher::*;

View File

@@ -0,0 +1,36 @@
use std::collections::HashMap;
/// A VfsItem represents either a file or directory as it came from the filesystem.
///
/// The interface here is intentionally simplified to make it easier to traverse
/// files that have been read into memory.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum VfsItem {
File {
route: Vec<String>,
file_name: String,
contents: String,
},
Dir {
route: Vec<String>,
file_name: String,
children: HashMap<String, VfsItem>,
},
}
impl VfsItem {
pub fn name(&self) -> &String {
match self {
&VfsItem::File { ref file_name , .. } => file_name,
&VfsItem::Dir { ref file_name , .. } => file_name,
}
}
pub fn route(&self) -> &[String] {
match self {
&VfsItem::File { ref route, .. } => route,
&VfsItem::Dir { ref route, .. } => route,
}
}
}

View File

@@ -0,0 +1,220 @@
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::time::Instant;
use plugin::PluginChain;
use vfs::VfsItem;
/// Represents a virtual layer over multiple parts of the filesystem.
///
/// Paths in this system are represented as slices of strings, and are always
/// relative to a partition, which is an absolute path into the real filesystem.
pub struct VfsSession {
/// Contains all of the partitions mounted by the Vfs.
///
/// These must be absolute paths!
partitions: HashMap<String, PathBuf>,
/// A chronologically-sorted list of routes that changed since the Vfs was
/// created, along with a timestamp denoting when.
change_history: Vec<VfsChange>,
/// When the Vfs was initialized; used for change tracking.
start_time: Instant,
plugin_chain: &'static PluginChain,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VfsChange {
timestamp: f64,
route: Vec<String>,
}
impl VfsSession {
pub fn new(plugin_chain: &'static PluginChain) -> VfsSession {
VfsSession {
partitions: HashMap::new(),
start_time: Instant::now(),
change_history: Vec::new(),
plugin_chain,
}
}
pub fn get_partitions(&self) -> &HashMap<String, PathBuf> {
&self.partitions
}
pub fn insert_partition<P: Into<PathBuf>>(&mut self, name: &str, path: P) {
let path = path.into();
assert!(path.is_absolute());
self.partitions.insert(name.to_string(), path.into());
}
fn route_to_path(&self, route: &[String]) -> Option<PathBuf> {
let (partition_name, rest) = match route.split_first() {
Some((first, rest)) => (first, rest),
None => return None,
};
let partition = match self.partitions.get(partition_name) {
Some(v) => v,
None => return None,
};
// It's possible that the partition points to a file if `rest` is empty.
// Joining "" onto a path will put a trailing slash on, which causes
// file reads to fail.
let full_path = if rest.is_empty() {
partition.clone()
} else {
let joined = rest.join("/");
let relative = Path::new(&joined);
partition.join(relative)
};
Some(full_path)
}
fn read_dir<P: AsRef<Path>>(&self, route: &[String], path: P) -> Result<VfsItem, ()> {
let path = path.as_ref();
let reader = match fs::read_dir(path) {
Ok(v) => v,
Err(_) => return Err(()),
};
let mut children = HashMap::new();
for entry in reader {
let entry = match entry {
Ok(v) => v,
Err(_) => return Err(()),
};
let path = entry.path();
let name = path.file_name().unwrap().to_string_lossy().into_owned();
let mut child_route = route.iter().cloned().collect::<Vec<_>>();
child_route.push(name.clone());
match self.read_path(&child_route, &path) {
Ok(child_item) => {
children.insert(name, child_item);
},
Err(_) => {},
}
}
let file_name = path.file_name().unwrap().to_string_lossy().into_owned();
Ok(VfsItem::Dir {
route: route.iter().cloned().collect::<Vec<_>>(),
file_name,
children,
})
}
fn read_file<P: AsRef<Path>>(&self, route: &[String], path: P) -> Result<VfsItem, ()> {
let path = path.as_ref();
let mut file = match File::open(path) {
Ok(v) => v,
Err(_) => return Err(()),
};
let mut contents = String::new();
match file.read_to_string(&mut contents) {
Ok(_) => {},
Err(_) => return Err(()),
}
let file_name = path.file_name().unwrap().to_string_lossy().into_owned();
Ok(VfsItem::File {
route: route.iter().cloned().collect::<Vec<_>>(),
file_name,
contents,
})
}
fn read_path<P: AsRef<Path>>(&self, route: &[String], path: P) -> Result<VfsItem, ()> {
let path = path.as_ref();
let metadata = match fs::metadata(path) {
Ok(v) => v,
Err(_) => return Err(()),
};
if metadata.is_dir() {
self.read_dir(route, path)
} else if metadata.is_file() {
self.read_file(route, path)
} else {
Err(())
}
}
/// Get the current time, used for logging timestamps for file changes.
pub fn current_time(&self) -> f64 {
let elapsed = self.start_time.elapsed();
elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 * 1e-9
}
/// Register a new change to the filesystem at the given timestamp and VFS
/// route.
pub fn add_change(&mut self, timestamp: f64, route: Vec<String>) {
match self.plugin_chain.handle_file_change(&route) {
Some(routes) => {
for route in routes {
self.change_history.push(VfsChange {
timestamp,
route,
});
}
},
None => {}
}
}
/// Collect a list of changes that occured since the given timestamp.
pub fn changes_since(&self, timestamp: f64) -> &[VfsChange] {
let mut marker: Option<usize> = None;
for (index, value) in self.change_history.iter().enumerate().rev() {
if value.timestamp >= timestamp {
marker = Some(index);
} else {
break;
}
}
if let Some(index) = marker {
&self.change_history[index..]
} else {
&self.change_history[..0]
}
}
/// Read an item from the filesystem using the given VFS route.
pub fn read(&self, route: &[String]) -> Result<VfsItem, ()> {
match self.route_to_path(route) {
Some(path) => self.read_path(route, &path),
None => Err(()),
}
}
pub fn write(&self, _route: &[String], _item: VfsItem) -> Result<(), ()> {
unimplemented!()
}
pub fn delete(&self, _route: &[String]) -> Result<(), ()> {
unimplemented!()
}
}

View File

@@ -0,0 +1,109 @@
use std::path::PathBuf;
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use std::time::Duration;
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
use pathext::path_to_route;
use vfs::VfsSession;
/// An object that registers watchers on the real filesystem and relays those
/// changes to the virtual filesystem layer.
pub struct VfsWatcher {
vfs: Arc<Mutex<VfsSession>>,
}
impl VfsWatcher {
pub fn new(vfs: Arc<Mutex<VfsSession>>) -> VfsWatcher {
VfsWatcher {
vfs,
}
}
fn start_watcher(
vfs: Arc<Mutex<VfsSession>>,
rx: mpsc::Receiver<DebouncedEvent>,
partition_name: String,
root_path: PathBuf,
) {
loop {
let event = rx.recv().unwrap();
let mut vfs = vfs.lock().unwrap();
let current_time = vfs.current_time();
match event {
DebouncedEvent::Write(ref change_path) |
DebouncedEvent::Create(ref change_path) |
DebouncedEvent::Remove(ref change_path) => {
if let Some(mut route) = path_to_route(&root_path, change_path) {
route.insert(0, partition_name.clone());
vfs.add_change(current_time, route);
} else {
eprintln!("Failed to get route from {}", change_path.display());
}
},
DebouncedEvent::Rename(ref from_change, ref to_change) => {
if let Some(mut route) = path_to_route(&root_path, from_change) {
route.insert(0, partition_name.clone());
vfs.add_change(current_time, route);
} else {
eprintln!("Failed to get route from {}", from_change.display());
}
if let Some(mut route) = path_to_route(&root_path, to_change) {
route.insert(0, partition_name.clone());
vfs.add_change(current_time, route);
} else {
eprintln!("Failed to get route from {}", to_change.display());
}
},
_ => {},
}
}
}
pub fn start(self) {
let mut watchers = Vec::new();
// Create an extra scope so that `vfs` gets dropped and unlocked
{
let vfs = self.vfs.lock().unwrap();
for (ref partition_name, ref root_path) in vfs.get_partitions() {
let (tx, rx) = mpsc::channel();
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(200))
.expect("Unable to create watcher! This is a bug in Rojo.");
match watcher.watch(&root_path, RecursiveMode::Recursive) {
Ok(_) => (),
Err(_) => {
eprintln!("WARNING: Unable to watch partition {}, with path {}\nMake sure that it's a file or directory.", partition_name, root_path.display());
continue;
},
}
watchers.push(watcher);
{
let partition_name = partition_name.to_string();
let root_path = root_path.to_path_buf();
let vfs = self.vfs.clone();
thread::spawn(move || {
Self::start_watcher(vfs, rx, partition_name, root_path);
});
}
}
}
loop {
thread::park();
}
}
}

View File

@@ -1,141 +0,0 @@
use std::{
fmt,
io::Write,
path::Path,
process::{Command, Stdio},
};
use rbx_tree::RbxId;
use crate::{
imfs::{Imfs, ImfsItem},
rbx_session::RbxSession,
};
static GRAPHVIZ_HEADER: &str = r#"
digraph RojoTree {
rankdir = "LR";
graph [
ranksep = "0.7",
nodesep = "0.5",
];
node [
fontname = "Hack",
shape = "record",
];
"#;
pub fn graphviz_to_svg(source: &str) -> String {
let mut child = Command::new("dot")
.arg("-Tsvg")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("Failed to spawn GraphViz process -- make sure it's installed in order to use /api/visualize");
{
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
stdin.write_all(source.as_bytes()).expect("Failed to write to stdin");
}
let output = child.wait_with_output().expect("Failed to read stdout");
String::from_utf8(output.stdout).expect("Failed to parse stdout as UTF-8")
}
pub struct VisualizeRbxSession<'a>(pub &'a RbxSession);
impl<'a> fmt::Display for VisualizeRbxSession<'a> {
fn fmt(&self, output: &mut fmt::Formatter) -> fmt::Result {
writeln!(output, "{}", GRAPHVIZ_HEADER)?;
visualize_rbx_node(self.0, self.0.get_tree().get_root_id(), output)?;
writeln!(output, "}}")?;
Ok(())
}
}
fn visualize_rbx_node(session: &RbxSession, id: RbxId, output: &mut fmt::Formatter) -> fmt::Result {
let node = session.get_tree().get_instance(id).unwrap();
let mut node_label = format!("{}|{}|{}", node.name, node.class_name, id);
if let Some(metadata) = session.get_instance_metadata(id) {
node_label.push('|');
node_label.push_str(&serde_json::to_string(metadata).unwrap());
}
node_label = node_label
.replace("\"", "&quot;")
.replace("{", "\\{")
.replace("}", "\\}");
writeln!(output, " \"{}\" [label=\"{}\"]", id, node_label)?;
for &child_id in node.get_children_ids() {
writeln!(output, " \"{}\" -> \"{}\"", id, child_id)?;
visualize_rbx_node(session, child_id, output)?;
}
Ok(())
}
pub struct VisualizeImfs<'a>(pub &'a Imfs);
impl<'a> fmt::Display for VisualizeImfs<'a> {
fn fmt(&self, output: &mut fmt::Formatter) -> fmt::Result {
writeln!(output, "{}", GRAPHVIZ_HEADER)?;
for root_path in self.0.get_roots() {
visualize_root_path(self.0, root_path, output)?;
}
writeln!(output, "}}")?;
Ok(())
}
}
fn normalize_name(path: &Path) -> String {
path.to_str().unwrap().replace("\\", "/")
}
fn visualize_root_path(imfs: &Imfs, path: &Path, output: &mut fmt::Formatter) -> fmt::Result {
let normalized_name = normalize_name(path);
let item = imfs.get(path).unwrap();
writeln!(output, " \"{}\"", normalized_name)?;
match item {
ImfsItem::File(_) => {},
ImfsItem::Directory(directory) => {
for child_path in &directory.children {
writeln!(output, " \"{}\" -> \"{}\"", normalized_name, normalize_name(child_path))?;
visualize_path(imfs, child_path, output)?;
}
},
}
Ok(())
}
fn visualize_path(imfs: &Imfs, path: &Path, output: &mut fmt::Formatter) -> fmt::Result {
let normalized_name = normalize_name(path);
let short_name = path.file_name().unwrap().to_string_lossy();
let item = imfs.get(path).unwrap();
writeln!(output, " \"{}\" [label = \"{}\"]", normalized_name, short_name)?;
match item {
ImfsItem::File(_) => {},
ImfsItem::Directory(directory) => {
for child_path in &directory.children {
writeln!(output, " \"{}\" -> \"{}\"", normalized_name, normalize_name(child_path))?;
visualize_path(imfs, child_path, output)?;
}
},
}
Ok(())
}

Some files were not shown because too many files have changed in this diff Show More