MENU
Главная » Файлы » Скрипты

Unity3D предупреждение пиратства.
08.05.2012, 06:28

Этот вид очень простой защиты особенно важен, если вы
пытаетесь превратить в деньги Вашу веб - игру и не хотите, чтобы
распространился по всем сайтам, где вы не видите никакой финансовой
компенсации.



Как использовать: Скопируйте и вставьте этот код в новый
сценарий C # с именем AntiPiracy.cs, а затем наложить скрипт на одном из ваших
игровых объектов, таких как плеер, или в главном меню. Вам нужно всего лишь
использовать его один раз.



Вы можете указать, следует ли разрешить работу на локальном
хосте с простым флажок. Это позволит вам проверить вашу игру в веб - браузере с
помощью построения и запуска (CTRL + B on Windows), не делает ничего
особенного.



Если тест на пиратство, необходимо указать URL-адрес для веб
- браузера для перенаправления игрока. Это может быть что угодно, но, вероятно,
заходит на сайт игры или нужную страницу для игры.



Вы можете вручную вызвать пиратства тест с помощью вызова
функции TestPiracy (). Вы можете проверить в начале каждого уровня, если вы
этого хотите. Хотя одного теста, как правило, достаточно.



Скрипт запускается автоматически при запуске. Вы можете
отключить этот режим, комментируя свою функцию Start (), а затем вручную
вызвать тест пиратства.



При создании сборки релиза я предлагаю вам отключить
разрешение на локальную машину инспектора, если вы хотите, чтобы ваши игроки
смогли скачать и играть в вашу игру со своего рабочего стола.



Как это работает: скрипт содержит список сайтов, разрешенных
для размещения игры, а если оно не совпадает с одной из разрешенных веб-сайтов,
которые вы указали, то это отражается у игрока веб-страницы, которую вы
указали.



Лицензия: Это GNU Lesser GPL. Это означает, что вы можете взять
его, изменить его, положить его в свою игру, и так далее. Вы должны оставить
уведомление об авторских правах на месте, если вы создали модифицированную
версию и распространяете её, то вы должны предоставить исходный код. Вы не
можете продать антипиратский сценарий, но вы можете включить его в продукт,
который продается.


Code
/*-----------------------------------------------------------------------------
  * AntiPiracy.cs - Permits the game only to run on allowed hosts
  * Copyright (C) 2010 Justin Lloyd
  * http://www.otakunozoku.com/
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 3 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU lesser General Public License
  * along with this library. If not, see <http://www.gnu.org/licenses/>.
  *
-----------------------------------------------------------------------------*/

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Text;

public class AntiPiracy : MonoBehaviour
{
  /// <summary>
  /// Do we permit execution from local host or local file system?
  /// </summary>
  public bool m_permitLocalHost = true;

  /// <summary>
  /// List of permitted remote hosts that host this game.
  /// </summary>
  public string[] m_permittedRemoteHosts;

  /// <summary>
  /// List of permitted localhost URLs
  /// </summary>
  public string[] m_permittedLocalHosts = { "file://", "http://localhost/",
"http://localhost:", "https://localhost/", "https://localhost:" };

  /// <summary>
  /// URL to bounce the player to if they are executing the game from an
unknown URL.
  /// </summary>
  public string m_bounceToURL;

  void Start()
  {
  TestPiracy();
  }

  /// <summary>
  /// Determine if the current host exists in the given list of permitted
hosts.
  /// </summary>
  /// <param name="hosts">An array of hosts permitted to host this game.
</param>
  /// <returns>True if the current host is permitted to host this game.
</returns>
  private bool IsValidHost(string[] hosts)
  {
  // print out list of hosts in debugging build
  if (Debug.isDebugBuild)
  {
  StringBuilder msg = new StringBuilder();
  msg.Append("Checking against list of hosts: ");
  foreach (string url in hosts)
  {
  msg.Append(url);
  msg.Append(",");
  }

  Debug.Log(msg.ToString());
  }

  // check current host against each of the given hosts
  foreach (string host in hosts)
  {
  if (Application.absoluteURL.IndexOf(host) == 0)
  {
  return true;
  }

  }

  return false;
  }

  /// <summary>
  /// Determine if the current host is a valid local host.
  /// </summary>
  /// <returns>True if the game is permitted to execute from local host and
  /// the current host is local host.</returns>
  public bool IsValidLocalHost()
  {
  if (m_permitLocalHost)
  {
  return IsValidHost(m_permittedLocalHosts);
  }

  return false;
  }

  /// <summary>
  /// Determine if the current host is a valid remote host.
  /// </summary>
  /// <returns>True if the game is permitted to execute from the remote host.
</returns>
  public bool IsValidRemoteHost()
  {
  return IsValidHost(m_permittedRemoteHosts);
  }

  /// <summary>
  /// Bounce the player to game's home page
  /// </summary>
  public void Bounce()
  {
  Application.OpenURL(m_bounceToURL);
  }

  /// <summary>
  /// Determine if the current host is a valid host (local or remote)
  /// </summary>
  /// <returns>True if the current host is permitted to host the game.
</returns>
  public bool IsValidHost()
  {
  if (IsValidLocalHost() == true)
  {
  return true;
  }

  if (IsValidRemoteHost() == true)
  {
  return true;
  }

  return false;
  }

  /// <summary>
  /// Compile a list of hosts in to a fragment of JavaScript.
  /// </summary>
  /// <param name="permittedHosts">List of hosts permitted to host the
game.</param>
  /// <returns>Fragment of JavaScript for testing the current host.
</returns>
  private string CompileHosts(string[] permittedHosts)
  {
  StringBuilder hosts = new StringBuilder();

  for (int i = 0; i < permittedHosts.Length; i++)
  {
  hosts.Append("(document.location.host != '");
  string url = permittedHosts[i];
  if (url.IndexOf("http://") == 0)
  {
  url = url.Substring(7);
  }
  else if (url.IndexOf("https://") == 0)
  {
  url = url.Substring(8);
  }

  hosts.Append(url);
  hosts.Append("')");
  if (i < permittedHosts.Length - 1)
  {
  hosts.Append(" && ");
  }

  }

  return hosts.ToString();
  }

  /// <summary>
  /// Perform a browser check using JavaScript to determine if the current
  /// host is permitted to host the game.
  /// </summary>
  private void CheckWithJavaScript()
  {
  StringBuilder javascriptTest = new StringBuilder();

  javascriptTest.Append("if (");
  // compile test for local hosts
  if (m_permitLocalHost)
  {
  javascriptTest.Append("(document.location.host != 'localhost') &&
(document.location.host != '')");
  if (m_permittedRemoteHosts.Length > 0)
  {
  javascriptTest.Append(" && ");
  }

  }

  // compile test for remote hosts
  javascriptTest.Append(CompileHosts(m_permittedRemoteHosts));
  javascriptTest.Append("){ document.location='");
  javascriptTest.Append(m_bounceToURL);
  javascriptTest.Append("'; }");
  if (Debug.isDebugBuild)
  {
  Debug.Log(javascriptTest);
  }

  Application.ExternalEval(javascriptTest.ToString());
  }

  /// <summary>
  /// Perform a complete check to see if the current host is permitted to
  /// host the game. Bounce the player to the game's home page if it is not.
  /// </summary>
  public void TestPiracy()
  {
  if (Debug.isDebugBuild)
  {
  Debug.Log(String.Format("The absolute URL of the application is
{0}", Application.absoluteURL));
  }

  if (Application.platform != RuntimePlatform.WindowsWebPlayer &&
Application.platform != RuntimePlatform.OSXWebPlayer)
  {
  Debug.Log("Testing for piracy but not in web browser, so not
worrying about it.");
  return;
  }

  // if it's not a valid remote host, bounce the user to the proper URL
  if (IsValidHost() == false)
  {
  if (Debug.isDebugBuild)
  {
  Debug.Log(String.Format("Failed valid remote host test.
Bouncing player to {0}", m_bounceToURL));
  }

  Bounce();
  return;
  }

  // it might appear to be a valid local or remote host, but one final
check in JavaScript to verify that
  CheckWithJavaScript();
  }

}
Категория: Скрипты | Добавил: DeMaN_74
Просмотров: 4913 | Загрузок: 0 | Рейтинг: 5.0/3
Всего комментариев: 0
Добавлять комментарии могут только зарегистрированные пользователи.
[ Регистрация | Вход ]
Хостинг от uCoz